ARC 73 E - Ball Coloring

E - Ball Coloring


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

There are N bags, each containing two white balls. The i-th box contains two balls with integers xi and yi written on them, respectively.

For each of these bags, you will paint one of the balls red, and paint the other blue.

Afterwards, the 2N balls will be classified according to color.

Then, we will define the following:

  • Rmax: the maximum integer written on a ball painted in red
  • Rmin: the minimum integer written on a ball painted in red
  • Bmax: the maximum integer written on a ball painted in blue
  • Bmin: the minimum integer written on a ball painted in blue

Find the minimum possible value of (RmaxRmin)×(BmaxBmin).

Constraints

  • 1≤N≤200,000
  • 1≤xi,yi≤109

Input

Input is given from Standard Input in the following format:

N
x1 y1
x2 y2
:
xN yN

Output

Print the minimum possible value.


Sample Input 1

3
1 2
3 4
5 6

Sample Output 1

15

The optimal solution is to paint the balls with x1x2y3 red, and paint the balls with y1y2x3 blue.


Sample Input 2

3
1010 10
1000 1
20 1020

Sample Output 2

380

Sample Input 3

2
1 1
1000000000 1000000000

Sample Output 3

999999998000000001
分析:考虑最大值和最小值有两种情况;
   1)最大值和最小值不在同一个集合,那么最大值所在集合最小值尽可能大,最小值所在集合最大值尽可能小;
     那么每组数小值放小集合,大值放大集合;
   2)最大值和最小值在同一个集合,考虑另一个集合,这个集合的最大值和最小值尽可能接近;
     把小值排序,依次用大值替换,维护最优解即可;反证法可以证明最优解;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000009
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=2e5+10;
const int N=2e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,a[maxn],b[maxn],id[maxn],ama,bma,ami=1e9,bmi=1e9,dma,dmi=1e9;
bool cmp(int x,int y){return a[x]<a[y];}
int main()
{
    int i,j;
    scanf("%d",&n);
    rep(i,1,n)
    {
        scanf("%d%d",&a[i],&b[i]);
        if(a[i]>b[i])swap(a[i],b[i]);
        id[i]=i;
        ama=max(ama,a[i]);
        ami=min(ami,a[i]);
        bma=max(bma,b[i]);
        bmi=min(bmi,b[i]);
    }
    ll ret=(ll)(ama-ami)*(bma-bmi);
    sort(id+1,id+n+1,cmp);
    rep(i,1,n)
    {
        dma=max(dma,b[id[i]]);
        dmi=min(dmi,b[id[i]]);
        ret=min(ret,(ll)(max(dma,a[id[n]])-min(dmi,a[id[i+1]]))*(bma-ami));
    }
    printf("%lld\n",ret);
    return 0;
}
posted @ 2017-05-03 21:56  mxzf0213  阅读(392)  评论(0编辑  收藏  举报