CF 529B Group Photo 2 (online mirror version)
解题思路
这道题要用到贪心的思路,首先要枚举一个h的最大值,之后check。如果这个东西的w[i]与h[i]都大于枚举的值就直接return false,如果w[i]比这个值小,h[i]比这个值大,就将h[i]与w[i]交换,注意并不能直接交换因为后面还要用到,要在答案中将h[i]加上。如果交换次数比n/2大也return false,如果w[i]与h[i]均比这个值小,则将w[i]-h[i]加入数组。后面将它排序,选出可以被交换的更新答案。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
const int MAXN = 1005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,w[MAXN],h[MAXN];
int ans=1e18+1,sum;
int cnt,upd[MAXN];
inline bool cmp(int x,int y){
return x>y;
}
inline bool check(int x){
int k=0;cnt=0;
for(register int i=1;i<=n;i++){
if(w[i]>x && h[i]>x) return false;
if(h[i]>x && w[i]<=x) k++,sum+=h[i],sum-=w[i];
else if(h[i]<=x && w[i]<=x) upd[++cnt]=w[i]-h[i];
sum+=w[i];
}
if(k>n/2) return false;
sort(upd+1,upd+1+cnt,cmp);
int res=n/2-k;
for(register int i=1;i<=res && i<=cnt;i++){
if(upd[i]<=0) break;
sum-=upd[i];
}
sum*=x;
return true;
}
signed main(){
n=rd();
int mx=0;
for(register int i=1;i<=n;i++)
w[i]=rd(),h[i]=rd(),mx=max(w[i],max(mx,h[i]));
for(register int i=1;i<=mx;i++){
sum=0;
if(!check(i)) continue;
ans=min(ans,sum);
}
printf("%lld",ans);
return 0;
}
CF 529B Group Photo 2 (online mirror version)的更多相关文章
- codeforce Group Photo 2 (online mirror version)
题目大意: 有n个矩形在地上排成一列,不可重叠,已知他们的宽度w和高度h,现在使至多[n / 2]个矩形旋转90度,问最后可以用多小的矩形恰好覆盖这n个矩形,求满足条件的最小矩形面积. n, w, h ...
- CF529B 【Group Photo 2 (online mirror version)】
贪心枚举最后方案中最大的h,设为maxh若某个人i的wi与hi均大于maxh,则此方案不可行若某个人恰有一个属性大于maxh,则可确定他是否换属性剩下的人按wi-hi从大到小排序后贪心选择O(nlog ...
- [CodeForces]529B Group Photo 2
AK爷GhostCai的电脑又蓝屏了Orz 贪心题,确定一个maxh,限定h不大于一个值.枚举maxh. check的时候的细节很多: 1.h>maxh但w<maxh交换的时候需要占用交换 ...
- 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 ...
随机推荐
- css3 ---1 基本的选择器
基本的选择器 <style type="text/css"> /*通配符选择器*/ * { margin: ; padding: ; border: none; } / ...
- 【CQOI2015】选数
题面 Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次 ...
- Atcoder arc096
C:Half and Half 几个if语句贪心算一算就好了 #include<cstdio> #include<algorithm> using namespace std; ...
- 关于python程序在VS code中运行时提示文件无法找到的报错
经过测试,在设置文件夹目录时,可以找到当前目录下的htm文件,采用with open()语句可以正常执行程序,如下图. 而当未设置当前目录,直接用vscode执行该程序时,就会报错文件无法找到File ...
- Apache添加多端口及实现单ip多端口映射的方法
这篇文章主要介绍了Apache添加多端口及实现单ip多端口映射的方法的相关资料,需要的朋友可以参考下(http://www.0831jl.com) 先给大家说下apache添加多端口的方法,具体步骤如 ...
- 廖雪峰Java11多线程编程-3高级concurrent包-5Atomic
Atomic java.util.concurrent.atomic提供了一组原子类型操作: 如AtomicInteger提供了 int addAndGet(int delta) int increm ...
- centos6 php7 安装 memcache 和 memcached
下载安装memcache 注意:官网的memcache包,暂时好像不支持php7.所以到下面地址下载memcache包,切换到php7分支 php7 memcache github 下载地址 官网下载 ...
- memcached 技术支持
1. Install sudo apt-get install memcached 2.启动和停止 启动: service memcached start 停止: service memcached ...
- 深入浅出 Java Concurrency (26): 并发容器 part 11 Exchanger[转]
可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视为 Synchro ...
- TmodJs:常用语法
ylbtech-TmodJs:常用语法 1.返回顶部 1.循环 {{each items as item index}} <tr> <td>{{index+1}}</td ...