贪心
枚举最后方案中最大的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. C标准库函数--文件IO操作函数。

    C标准库文件读写函数总结:都是对文件流进行输入输出的函数分为对文件的有格式读写以及无格式读写 一.文件的无格式读写根据每次读写字符的数量,分为三类:1.按字符读写文件 按字符读有三个函数:以下三个函数 ...

  2. 2017年Java面试题整理

    原文出处:CSDN邓帅 面试是我们每个人都要经历的事情,大部分人且不止一次,这里给大家总结最新的2016年面试题,让大家在找工作时候能够事半功倍. 1.Switch能否用string做参数? a.在 ...

  3. bzoj千题计划149:bzoj2527: [Poi2011]Meteors

    http://www.lydsy.com/JudgeOnline/problem.php?id=2527 整体二分 区间加,单点查,树状数组维护差分序列 注意 累积可能会爆long long,所以一满 ...

  4. POJ 3308 Paratroopers(最小点权覆盖)(对数乘转加)

    http://poj.org/problem?id=3308 r*c的地图 每一个大炮可以消灭一行一列的敌人 安装消灭第i行的大炮花费是ri 安装消灭第j行的大炮花费是ci 已知敌人坐标,同时消灭所有 ...

  5. 51 nod 1058 N的阶乘的长度

    1058 N的阶乘的长度 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.   In ...

  6. Nginx错误日志与优化专题

    一.Nginx配置和内核优化 实现突破十万并发 二.一次Nignx的502页面的错误记录 (1)错误页面显示 错误日志: // :: [error] #: * recv() failed (: Con ...

  7. python学习笔记2--list

    一.list操作 list=['xiaohei','xiaobai','xiaohong']#定义一个数组 name=[]#定义一个空数组 #查 #下标是从0开始 print(list[1]) #-1 ...

  8. .NET 定时器类及使用方法

    Timer类实现定时任务 //2秒后开启该线程,然后每隔4s调用一次 System.Threading.Timer timer = new System.Threading.Timer((n) =&g ...

  9. 【译】第八篇 SQL Server代理使用外部程序

    本篇文章是SQL Server代理系列的第八篇,详细内容请参考原文 在这一系列的上一篇,学习了如何用SQL Server代理作业活动监视器监控作业活动和查看作业历史记录.在实时监控和管理SQL Ser ...

  10. GRUB (简体中文)

    原文链接:https://wiki.archlinux.org/index.php/GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 前言 引导程序是计算机启动时 ...