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

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
int n,w[maxn],h[maxn],hh,ans=1e9,sw;
bool cmp(const int &a,const int &b)
{
return w[a]-h[a]>w[b]-h[b]; //按wi-hi排序
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&w[i],&h[i]);
hh=max(w[i],max(hh,h[i])),sw+=w[i];
}
for(int maxh=;maxh<=hh;maxh++)
{
int cnt=,rs=sw;
vector<int>v;
for(int i=;i<=n;i++) //分类讨论
{
if(w[i]>maxh&&h[i]>maxh)
{
cnt=1e9;
break;
}
else if(w[i]>maxh&&h[i]<=maxh)
continue;
else if(w[i]<=maxh&&h[i]>maxh)
{
rs+=h[i]-w[i];
cnt++;
}
else if(w[i]>h[i])
v.push_back(i); //把待处理元素放入vector
}
if(cnt>n/)
continue;
sort(v.begin(),v.end(),cmp);
for(int i=;i<v.size()&&cnt+i<n/;i++)
rs+=h[v[i]]-w[v[i]];
ans=min(ans,rs*maxh);
}
printf("%d\n",ans);
return ;
}

CF529B 【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. CF 529B Group Photo 2 (online mirror version)

    传送门 解题思路 这道题要用到贪心的思路,首先要枚举一个h的最大值,之后check.如果这个东西的w[i]与h[i]都大于枚举的值就直接return false,如果w[i]比这个值小,h[i]比这个 ...

  3. PAT甲级——A1109 Group Photo【25】

    Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...

  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. bzoj 1406

    %%% PoPoQQQ x^2=kn+1 x^2-1=kn (x+1)(x-1)=kn 令x+1=k1*n1,x-1=k2*n2,其中k1k2=k,n1n2=n 因此我们可以枚举n的约数中所有大于等于 ...

  2. bzoj 3190 赛车 半平面交

    直接写的裸的半平面交,已经有点背不过模板了... 这题卡精度,要用long double ,esp设1e-20... #include<iostream> #include<cstd ...

  3. 「Vue」过滤器

    #全局过滤器要写在var vue之前<td>{{item.time | ctime }}</td>Vue.filter('ctime'(过滤器名),function(data( ...

  4. P4888 三去矩阵

    P4888 三去矩阵 给出一个字符矩阵, 多次询问求以 \((x, y)\) 为中心的最长回文串长度(即横竖两种) \(l, q <= 2000\) Solution 数据范围小直接模拟即可 C ...

  5. SQL2005函数大全

    表达式:是常量.变量.列或函数等与运算符的任意组合.以下参数中表达式类型是指表达式经运算后返回的值的类型 字符串函数 函数名称 参数 示例 说明 ascii (字符串表达式) select ascii ...

  6. vue相关安装命令

    安装cnpm npm install cnpm -g --registry=https://registry.npm.taobao.org

  7. KVM基本实现原理

    KVM 虚拟化技术概述 http://blog.csdn.net/yearn520/article/details/6461047 KVM 虚拟化技术在 AMD 平台上的实现 1.http://www ...

  8. 新建 Vue项目 使用iView报错解决

    错误如下: error in ./src/index.less Module build failed: // https://github.com/ant-design/ant-motion/iss ...

  9. 51 nod 1243 排船的问题

    1243 排船的问题http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 题目来源: Codility 基准时间限制:1 ...

  10. ConcurrentHashMap 产生NullPointerException

    今天测试在发给我一段报错日志后,根据日志定位到从ConcurrentHashMap 的缓存中get的时候,ConcurrentHashMap的底层抛出了空指针,当时感觉很奇怪为什么在get的时候产生空 ...