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 ...
随机推荐
- 13-7-return的高级使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MapReduce工作流程
- NuGet 命令行使用EntityFrameWork
初始化 Enable-migrations 迁移 Add-Migration Donator_Add_CreationTime 执行操作 UpDate-database 撤销更改 Update-Dat ...
- JS 标签页切换(复杂)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- Georgia and Bob
Georgia and Bob 给出一个严格递增的正整数数列\(\{a_i\}\),每一次操作可以对于其中任意一个数减去一个正整数,但仍然要保证数列的严格递增性,现在两名玩家轮流操作,不能操作的玩家判 ...
- STL vector容器需要警惕的一些坑
从迭代器中取值切记需要判断是否为空 例如: vector<int> vtTest; vtTest.clear(); if (vtTest.empty()){ ; } ]; 如果没有忘了判断 ...
- 二分图——poj2446匈牙利算法
/* 怎么建图: 首先分集合:不能相连的点必然在一个集合里,即对角点 再确定怎么连边: 一个点可以向上下左右连边,如果遇到了洞则不行 dfs(i),让匹配到的点接受i作为match结果 寻找增广路时, ...
- 集合划分——cf1028D思维题
非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...
- flask请求上下文源码分析
一.什么是上下文 每一段程序都有很多外部变量,只有像add这种简单的函数才是没有外部变量的,一旦你的一段程序有了外部变量,这段程序就不完整了,不能独立运行,你为了使他们能运行,就要给所有的外部变量一个 ...
- spring boot 监听容器启动
/** * 在容器启动的时候 加载没问完成的消息重发 * @author zhangyukun * */ @Component @Slf4j public class LoadMessageListe ...