CF529B 【Group Photo 2 (online mirror version)】
贪心
枚举最后方案中最大的h,设为maxh
若某个人i的wi与hi均大于maxh,则此方案不可行
若某个人恰有一个属性大于maxh,则可确定他是否换属性
剩下的人按wi-hi从大到小排序后贪心选择
O(nlogn+n2)
- #include<iostream>
- #include<cstdio>
- #include<vector>
- #include<algorithm>
- using namespace std;
- const int maxn=;
- int n,w[maxn],h[maxn],hh,ans=1e9,sw;
- bool cmp(const int &a,const int &b)
- {
- return w[a]-h[a]>w[b]-h[b]; //按wi-hi排序
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=;i<=n;i++)
- {
- scanf("%d%d",&w[i],&h[i]);
- hh=max(w[i],max(hh,h[i])),sw+=w[i];
- }
- for(int maxh=;maxh<=hh;maxh++)
- {
- int cnt=,rs=sw;
- vector<int>v;
- for(int i=;i<=n;i++) //分类讨论
- {
- if(w[i]>maxh&&h[i]>maxh)
- {
- cnt=1e9;
- break;
- }
- else if(w[i]>maxh&&h[i]<=maxh)
- continue;
- else if(w[i]<=maxh&&h[i]>maxh)
- {
- rs+=h[i]-w[i];
- cnt++;
- }
- else if(w[i]>h[i])
- v.push_back(i); //把待处理元素放入vector
- }
- if(cnt>n/)
- continue;
- sort(v.begin(),v.end(),cmp);
- for(int i=;i<v.size()&&cnt+i<n/;i++)
- rs+=h[v[i]]-w[v[i]];
- ans=min(ans,rs*maxh);
- }
- printf("%d\n",ans);
- return ;
- }
CF529B 【Group Photo 2 (online mirror version)】的更多相关文章
- codeforce Group Photo 2 (online mirror version)
题目大意: 有n个矩形在地上排成一列,不可重叠,已知他们的宽度w和高度h,现在使至多[n / 2]个矩形旋转90度,问最后可以用多小的矩形恰好覆盖这n个矩形,求满足条件的最小矩形面积. n, w, h ...
- CF 529B Group Photo 2 (online mirror version)
传送门 解题思路 这道题要用到贪心的思路,首先要枚举一个h的最大值,之后check.如果这个东西的w[i]与h[i]都大于枚举的值就直接return false,如果w[i]比这个值小,h[i]比这个 ...
- PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...
- A1109. Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- PAT A1109 Group Photo (25 分)——排序
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 1109 Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 1109 Group Photo (25 分)
1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...
- PAT 1109 Group Photo[仿真][难]
1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...
- 1109. Group Photo (25)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- golang interface 类型变量当作某个具体类型使用
比如,我们定义了一个 struct type person struct { Name string `json:"name"` Age int `json:"age&q ...
- python基础之生成器迭代器
1 生成器: 为什么要有生成器? 就拿列表来说吧,假如我们要创建一个list,这个list要求格式为:[1,4,9,16,25,36……]这么一直持续下去,直到有了一万个元素的时候为止.如果我们要创建 ...
- Consul 服务发现与配置
Consule 是什么 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul 的客户端可用提供一个服务,比如 api 或 ...
- 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 ...
- Java上传文件夹(Jersey)
背景介绍:公司要在CMS系统上为运营人员提供一个功能供运营人员将做好的活动页面上传到阿里云存储上,上传的内容为一个文件夹,文件夹内部有.html网页,JS文件夹下有JS文件,CSS文件夹下有样式表,I ...
- 2017 国庆湖南 Day1
卡特兰数 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ] ...
- hdu1286 找新朋友
找新朋友 http://acm.hdu.edu.cn/showproblem.php?pid=1286 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 100 Most Popular Machine Learning Video Talks
100 Most Popular Machine Learning Video Talks 26971 views, 1:00:45, Gaussian Process Basics, David ...
- asp启用父路径
开启父路径后可以用../来表示上一层目录,如果网站程序中使用了../,不开启则网站程序里有../就会报错. IIS6启用父路径方法:打开IIS管理器——网站——右键属性——主目录——配置——选项——选 ...
- 双11怎么那么强!之二:浅析淘宝网络通信库tbnet的实现
最近开始看Tair的源码实现,Tair的通信使用的是淘宝的开源的网络库tbnet实现.具体来说是依靠tbnet::Transport类型实现,其源代码路径如下:http://code.taobao.o ...