【Codeforces529B】【贪心】Group Photo 2

Group Photo 2

Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

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.

Samples

Input1

3
10 1
20 2
30 3

Output1

180

Input2

3
3 1
2 2
4 3

Output2

21

Input3

1
5 10

Output3

50

Source

VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)

这个题枚举h的最大值,然后先保证所有h不大于枚举值,然后贪心去求最小值,需要注意的就是枚举的范围应该是1到w和h的最大值,而不是h的最大值

代码见下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 1005

int h[M],w[M],th[M],tw[M],n,mx,k,ans=INF,sum,a[M];

void yuan()
{
    memcpy(th+1,h+1,4*n);
    memcpy(tw+1,w+1,4*n);
}

bool com(int a,int b)
{
    return a>b;
}

bool check(int x)
{
    int ti=k;
    for(int i=1;i<=n;i++)
        if(th[i]>x)
        {
            if(tw[i]<=x&&ti>0)
            {
                swap(tw[i],th[i]);
                ti--;
            }
            else return 0;
        }
    clr(a);
    sum=0;
    for(int i=1;i<=n;i++)
        sum+=tw[i];
    for(int i=1;i<=n;i++)
        if(tw[i]<=x)a[i]=tw[i]-th[i];
        else a[i]=-1;
    sort(a+1,a+n+1,com);
    for(int i=1;i<=n;i++)
    {
        if(a[i]>0&&ti>0)
        {
            sum-=a[i];
            ti--;
        }
    }
    return 1;
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    n=read();k=n/2;
    for(int i=1;i<=n;i++)
    {
        w[i]=read();
        h[i]=read();
        mx=max(mx,h[i]);
        mx=max(mx,w[i]);
    }
    for(int i=1;i<=mx;i++)
    {
        yuan();
        if(!check(i))continue;
        ans=min(ans,i*sum);
    }
    cout<<ans;
    return 0;
}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值