传送门

解题思路

这道题要用到贪心的思路,首先要枚举一个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. 2019-9-2-win10-uwp-弹起键盘不隐藏界面元素

    title author date CreateTime categories win10 uwp 弹起键盘不隐藏界面元素 lindexi 2019-09-02 12:57:38 +0800 2018 ...

  2. html--图片背景兼容,兼容IE6

    在IE6中对图片格式png24支持度不高, 如果使用的图片格式是png24,则会导致透明效果无法正常显示 解决方法: 1.可以使用png8来代替png24,即可解决问题, 但是使用png8代替png2 ...

  3. vertx使用过程中浏览器端Cookie重复问题

    [问题描述] 背景: 使用vertx提供的服务,使用Dispatcher做路由转发,内部通过routingcontext做请求传递及响应. 现象: 在谷歌浏览器的Network中,看到Cookie中有 ...

  4. HTML和css简单日常总结

    今天主要学习了两个部分:第一部分html:在w3school学习了一些标签的使用和一些属性和方法,例如一些标签<a>,<h1><h6><from>< ...

  5. CI框架 session 不能读取的问题,PHP7环境

    根本原因在这,libraries/Session/Session.php 中 128行: 如果sessionid的长度不是40的话,每次执行都会 unset($_COOKIE[ci_session]) ...

  6. 用Spire.PDF提取PDF里的PNG图片

    用Nuget抓取类库,FreeSpire.PDF就可以 代码如下 , 亲测可以抓取PNG图形,即使原图是JPG,也会存成PNG格式输出: //加载PDF文档 PdfDocument doc = new ...

  7. 设定计算属性setter

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  8. 《DSP using MATLAB》Problem 8.28

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. ES6之主要知识点(十)Proxy

    Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种“元编程”(meta programming),即对编程语言进行编程. Proxy 可以理解成,在目标对象之前架设一层“拦 ...

  10. 【心无旁骛】vuex-simple

    这个算是一个小的demo嘛,先放上开源github地址:https://github.com/sascha245/vuex-simple 倒是可以先看下效果 呃呃,因为这个项目所在的目录与平时我们一般 ...