传送门

解题思路

这道题要用到贪心的思路,首先要枚举一个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)的更多相关文章

  1. codeforce Group Photo 2 (online mirror version)

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

  2. CF529B 【Group Photo 2 (online mirror version)】

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

  3. [CodeForces]529B Group Photo 2

    AK爷GhostCai的电脑又蓝屏了Orz 贪心题,确定一个maxh,限定h不大于一个值.枚举maxh. check的时候的细节很多: 1.h>maxh但w<maxh交换的时候需要占用交换 ...

  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. javaweb中静态文件的处理方法

    方案一:激活Tomcat的defaultServlet来处理静态文件 在web.xml中添加: <servlet-mapping> <servlet-name>default& ...

  2. 获取m,n之间的随机整数

    获取m,n之间的随机整数 代码去下:

  3. 02.MyBatis在DAO层开发使用的Mapper动态代理方式

    在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...

  4. React在componentWillMount中请求接口数据结束后再执行render

    1.在getInitialState中初始化isloading,初始值false getInitialState() { return { editionid: '', isloading:false ...

  5. loj6247 九个太阳

    题意: k<=2^20,n<=10^15. 标程: #include<cstdio> using namespace std; typedef long long ll; ; ...

  6. js怎样截取以'-'分割的字符串

    在日期2019-09-01,怎样截取年只要月和日,下面是主要代码 var aa = '2019-09-01'; var bb = aa.split('-'); console.log(bb);//打印 ...

  7. springboot中pageHelper插件 list设置不进去 为null

    分页pageHelper中list放不进去值  为null,可能的解决方案如下: 1. 注意代码顺序,PageHelper.startPage(pageNumber,pageSize)要放在查询Lis ...

  8. Jmeter性能测试 入门【转】

    Jmeter性能测试 入门[转] Jmeter是一款优秀的开源测试工具, 是每个资深测试工程师,必须掌握的测试工具,熟练使用Jmeter能大大提高工作效率. 熟练使用Jmeter后, 能用Jmeter ...

  9. IOS学习笔记56-IOS7状态栏适配方法一

    近期由于IOS7的发布,所以应用的适配潮可谓是都搞的锣鼓喧天,甚是热闹,因此呢,因适配IOS7而产生的问题也是铺天盖地的卷来,所以了,我也从简单的状态栏适配开始,先研究了下关于状态栏的适配,特总结如下 ...

  10. 反编译之paktool工具

    1.官网配置mac教程https://ibotpeaches.github.io/Apktool/install/ (1)右键选择:链接储存为(命名:apktool,格式:选择所有文件): (2)下载 ...