贪心
枚举最后方案中最大的h,设为maxh
若某个人i的wi与hi均大于maxh,则此方案不可行
若某个人恰有一个属性大于maxh,则可确定他是否换属性
剩下的人按wi-hi从大到小排序后贪心选择
O(nlogn+n2)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. int n,w[maxn],h[maxn],hh,ans=1e9,sw;
  8. bool cmp(const int &a,const int &b)
  9. {
  10. return w[a]-h[a]>w[b]-h[b]; //按wi-hi排序
  11. }
  12. int main()
  13. {
  14. scanf("%d",&n);
  15. for(int i=;i<=n;i++)
  16. {
  17. scanf("%d%d",&w[i],&h[i]);
  18. hh=max(w[i],max(hh,h[i])),sw+=w[i];
  19. }
  20. for(int maxh=;maxh<=hh;maxh++)
  21. {
  22. int cnt=,rs=sw;
  23. vector<int>v;
  24. for(int i=;i<=n;i++) //分类讨论
  25. {
  26. if(w[i]>maxh&&h[i]>maxh)
  27. {
  28. cnt=1e9;
  29. break;
  30. }
  31. else if(w[i]>maxh&&h[i]<=maxh)
  32. continue;
  33. else if(w[i]<=maxh&&h[i]>maxh)
  34. {
  35. rs+=h[i]-w[i];
  36. cnt++;
  37. }
  38. else if(w[i]>h[i])
  39. v.push_back(i); //把待处理元素放入vector
  40. }
  41. if(cnt>n/)
  42. continue;
  43. sort(v.begin(),v.end(),cmp);
  44. for(int i=;i<v.size()&&cnt+i<n/;i++)
  45. rs+=h[v[i]]-w[v[i]];
  46. ans=min(ans,rs*maxh);
  47. }
  48. printf("%d\n",ans);
  49. return ;
  50. }

CF529B 【Group Photo 2 (online mirror version)】的更多相关文章

  1. codeforce Group Photo 2 (online mirror version)

    题目大意: 有n个矩形在地上排成一列,不可重叠,已知他们的宽度w和高度h,现在使至多[n / 2]个矩形旋转90度,问最后可以用多小的矩形恰好覆盖这n个矩形,求满足条件的最小矩形面积. n, w, h ...

  2. CF 529B Group Photo 2 (online mirror version)

    传送门 解题思路 这道题要用到贪心的思路,首先要枚举一个h的最大值,之后check.如果这个东西的w[i]与h[i]都大于枚举的值就直接return false,如果w[i]比这个值小,h[i]比这个 ...

  3. PAT甲级——A1109 Group Photo【25】

    Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...

  4. A1109. Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  5. PAT A1109 Group Photo (25 分)——排序

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  6. 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  7. 1109 Group Photo (25 分)

    1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...

  8. PAT 1109 Group Photo[仿真][难]

    1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...

  9. 1109. Group Photo (25)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

随机推荐

  1. golang interface 类型变量当作某个具体类型使用

    比如,我们定义了一个 struct type person struct { Name string `json:"name"` Age int `json:"age&q ...

  2. python基础之生成器迭代器

    1 生成器: 为什么要有生成器? 就拿列表来说吧,假如我们要创建一个list,这个list要求格式为:[1,4,9,16,25,36……]这么一直持续下去,直到有了一万个元素的时候为止.如果我们要创建 ...

  3. Consul 服务发现与配置

    Consule 是什么 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul 的客户端可用提供一个服务,比如 api 或 ...

  4. python net-snmp使用

    安装 官网:http://www.net-snmp.org/download.html 环境:CentOS 6.6 + python 2.7.10 1.下载安装包 net-snmp-5.6.2.1.t ...

  5. Java上传文件夹(Jersey)

    背景介绍:公司要在CMS系统上为运营人员提供一个功能供运营人员将做好的活动页面上传到阿里云存储上,上传的内容为一个文件夹,文件夹内部有.html网页,JS文件夹下有JS文件,CSS文件夹下有样式表,I ...

  6. 2017 国庆湖南 Day1

    卡特兰数 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ] ...

  7. hdu1286 找新朋友

    找新朋友 http://acm.hdu.edu.cn/showproblem.php?pid=1286 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  8. 100 Most Popular Machine Learning Video Talks

    100 Most Popular Machine Learning Video Talks 26971 views, 1:00:45,  Gaussian Process Basics, David ...

  9. asp启用父路径

    开启父路径后可以用../来表示上一层目录,如果网站程序中使用了../,不开启则网站程序里有../就会报错. IIS6启用父路径方法:打开IIS管理器——网站——右键属性——主目录——配置——选项——选 ...

  10. 双11怎么那么强!之二:浅析淘宝网络通信库tbnet的实现

    最近开始看Tair的源码实现,Tair的通信使用的是淘宝的开源的网络库tbnet实现.具体来说是依靠tbnet::Transport类型实现,其源代码路径如下:http://code.taobao.o ...