贪心
枚举最后方案中最大的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. 【arc073D】Many Moves

    Portal -->arc073D Description ​ 有\(n\)个格子,编号从左到右为\(1\sim n\),一开始有两个棋子,位置给定,接下来要依次进行\(Q\)次操作,第\(i\ ...

  2. laravel 中的 toSql 获取带参数的 sql 语句

    默认情况下,toSql 获取到的 sql 里面的参数使用 "?" 代替的,如下: DB::table('user')->where('id', 1)->toSql(); ...

  3. Eloquent 条件查询——tucker-eric/eloquentfilter 笔记

    请阅读 https://github.com/Tucker-Eric/EloquentFilter , 里面有很全的文档和注释,以下仅列出关键部分. 1. 安装 composer require tu ...

  4. Tomcat权威指南-读书摘要系列2

    2. 配置Tomcat 2.1. 重定向Web应用程序的目录 将工程文件与Tomcat分离 复制conf和webapps文件夹到分离目录: 配置CATALINA_BASE环境变量,值为分离目录: 2. ...

  5. IOS数组的排序和筛选

    1.排序 [self.tableItems sortUsingComparator:^NSComparisonResult(GPBTeacherBrief* obj1, GPBTeacherBrief ...

  6. html基础知识汇总(二)之Emmet语法

    div.imageBox+div.infoBox+input[type="button" class="starBtn"]*3 <div class=&q ...

  7. 64位Ubuntu 安装scrapy遇到的问题

    这两天准备开始学习Python爬虫,安装scrapy框架后出现 Traceback (most recent call last): File "/usr/local/bin/scrapy& ...

  8. java并发实践笔记

    底层的并发功能与并发语义不存在一一对应的关系.同步和条件等底层机制在实现应用层协议与策略须始终保持一致.(需要设计级别策略.----底层机制与设计级策略不一致问题). 简介 1.并发简史.(资源利用率 ...

  9. 在嵌入式Linux系统(OK6410)中移植Boa 服务器

    OK6410的Boa服务器移植: <一> Boa的编译 1. 从 www.boa.org 下载 Boa 服务器的最新版:boa-0.94.13.tar.gz. 2. 解压:tar xzf ...

  10. 29、HashSet简介

    Set的特点 Set里面存储的元素不能重复,没有索引,存取顺序不一致. package com.monkey1024.set; import java.util.HashSet; /** * Set的 ...