【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;
}

大概就是這個樣子,如果有什麼問題,或錯誤,請在評論區提出,謝謝。

發佈了76 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章