Codeforces 529B Group Photos 2 (one mirror version) [贪心]

B. Group Photo 2 (online mirror version)
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

Many years have passed, and n friends met at a party again. Technologies have leaped forward since the last meeting, cameras with timer appeared and now it is not obligatory for one of the friends to stand with a camera, and, thus, being absent on the photo.

Simply speaking, the process of photographing can be described as follows. Each friend occupies a rectangle of pixels on the photo: the i-th of them in a standing state occupies a wi pixels wide and a hi pixels high rectangle. But also, each person can lie down for the photo, and then he will occupy a hi pixels wide and a wi pixels high rectangle.

The total photo will have size W × H, where W is the total width of all the people rectangles, and H is the maximum of the heights. The friends want to determine what minimum area the group photo can they obtain if no more than n / 2 of them can lie on the ground (it would be strange if more than n / 2 gentlemen lie on the ground together, isn’t it?..)

Help them to achieve this goal.

Input
The first line contains integer n (1 ≤ n ≤ 1000) — the number of friends.

The next n lines have two integers wi, hi (1 ≤ wi, hi ≤ 1000) each, representing the size of the rectangle, corresponding to the i-th friend.

Output
Print a single integer equal to the minimum possible area of the photo containing all friends if no more than n / 2 of them can lie on the ground.

Examples
input
3
10 1
20 2
30 3
output
180
input
3
3 1
2 2
4 3
output
21
input
1
5 10
output
50


由于可旋转的数量有限,那么枚举最大的height

如果存在不论是否旋转都不能的人就continue
如果有一个大于lim,就通过旋转或者不旋转达到需要的height以内
如果两者都小于lim,那么加入优先队列,从中选取w-h最大的开始贪心,因为一开始totw不变,w-h越大对答案贡献越大

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;
struct Data
{
    int w,h;
    bool operator < (const Data t) const { return w - h < t.w - t.h; }
}a[maxn];
int n;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("photo.in","r",stdin);
    freopen("photo.out","w",stdout);
#endif
    scanf("%d",&n);
    int sum = 0;
    for(int i=1;i<=n;i++) scanf("%d%d",&a[i].w,&a[i].h),sum+=a[i].w;
    int ans = INF;
    for(int i=1;i<=n;i++)
    {
        priority_queue <int> que;
        int lim = a[i].h;
        bool flag = false;
        int totw = sum;
        int cnt = 0;
        for(int j=1;j<=n;j++) if(i^j)
            if(a[j].w>lim && a[j].h>lim)
            {
                flag = true;
                break;
            }
            else if(a[j].w > a[j].h && a[j].w <= lim) que.push(a[j].w - a[j].h);
            else if(a[j].w < a[j].h && a[j].h>lim) totw += a[j].h - a[j].w , cnt++;
        if(flag || cnt>(n>>1)) continue;
        while(!que.empty() && cnt<(n>>1))
        {
            int tmp = que.top(); que.pop();
            totw -= tmp;
            cnt++;
        }
        smin(ans,totw*lim);
    }
    for(int i=1;i<=n;i++)
    {
        int lim = a[i].w;
        int totw = sum; totw -= a[i].w-a[i].h;
        bool flag = false;
        int cnt = 1;
        priority_queue <int> que;
        for(int j=1;j<=n;j++) if(i^j)
            if(a[j].w>lim && a[j].h>lim)
            {
                flag = true;
                break;
            }
            else if(a[j].w > a[j].h && a[j].w <= lim) que.push(a[j].w - a[j].h);
            else if(a[j].w < a[j].h && a[j].h>lim) totw += a[j].h - a[j].w , cnt++;
        if(flag || cnt>(n>>1)) continue;
        while(!que.empty() && cnt<(n>>1))
        {
            int tmp = que.top(); que.pop();
            totw -= tmp;
            cnt++;
        }
        smin(ans,totw*lim);
    }
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值