传送门

解题思路

这道题要用到贪心的思路,首先要枚举一个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. Activit单元i测试(与spring集成测试)

    1.测试 eclipse下安装activiti插件以及maven 右键新建activiti project(这时会自动创建pom依赖以及activiti.cfg.xml,但还不是maven项目) 选中 ...

  2. Foundation框架系列-NSArray

    NSArray常用API 数组字符串指定字符拼接 // 将数组中的元素以separator拼接返回字符串 比如@[@"a=1", @"b=2"] 以separa ...

  3. select函数使用

    这两天写了这么一段代码,select直接返回-1,错误信息是“invalid argments”,显然没有达到阻塞超时的效果. 代码如下: bool IsSocketWaitRead(inf fd,i ...

  4. SPOJ694 New Distinct Substrings

    New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...

  5. html---三列布局

    三列布局 1一 1.两边固定 当中自适应 2.当中列要完整显示 3.当中列要优先加载 <!DOCTYPE html> <html> <head> <meta ...

  6. light oj 1098 数学规律

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  7. 使用WCF上传文件

              在WCF没出现之前,我一直使用用WebService来上传文件,我不知道别人为什么要这么做,因为我们的文件服务器和网站后台和网站前台都不在同一个机器,操作人员觉得用FTP传文件太麻 ...

  8. samba初级使用记录

    首先安利一下什么是samba: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在 ...

  9. disruptor 高效队列

    disruptor 是什么: disruptor 是一个 低延时的 无锁 环形 队列.  相较于 java的 队列 ,他有明显的优点  ,无界,无锁,低延时(解决了为内存共享问题 ) disrupto ...

  10. 关于获取webview(窗口间关系)的方法

    1.获取指定页面ID的webview plus.webview.getWebviewById('为页面设置的id值'): 该方法主要用于首页底部导航切换到子页面时不执行子页面的函数,因为在设置导航的时 ...