ARC 73 E - Ball Coloring

本文探讨了E-BallColoring问题的最优解策略,通过将球分为红蓝两组,使得(Rmax−Rmin)×(Bmax−Bmin)的值最小。分析了两种情况下的解决方案,并提供了实现代码。

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

转载于:https://www.cnblogs.com/dyzll/p/6804116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值