Ball Coloring

6552: Ball Coloring

时间限制: 1 Sec  内存限制: 128 MB
提交: 13  解决: 7
[提交][状态][讨论版][命题人:admin]

题目描述

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 (Rmax−Rmin)×(Bmax−Bmin).

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

输入

Input is given from Standard Input in the following format:
N
x1 y1
x2 y2
:
xN yN

输出

Print the minimum possible value.

样例输入

3
1 2
3 4
5 6

样例输出

15

提示

The optimal solution is to paint the balls with x1, x2, y3 red, and paint the balls with y1, y2, x3 blue.

思维题,解法:

考虑所有数中最大的和最小的:MINMAXMIN,MAX,它们要么在两个集合中,要么只在一个集合中。不失一般性,我们考虑以下两种情况: 
   
  1.Rmin=MINBmax=MAXRmin=MIN,Bmax=MAX 
  这时,我们要最小化RmaxRmax,最大化BminBmin,那么就只要在分组时把小的分给RR,大的分给BB即可。 
   
  2.Rmin=MINRmax=MAXRmin=MIN,Rmax=MAX 
  这时,只需最小化BmaxBminBmax−Bmin。这样的话我们知道肯定是要取,比如说2n2n个数一起排完序后,中间连续互斥的nn个。这样的话,我们先让每组中的xi<yixi<yi,然后按照xx数组排序,先让BB数组取x1xnx1∼xn,然后再逐个把xixi调成yiyi计算答案即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;
const long long INF=1e18+10;
typedef pair<long long,long long>PA;
vector<PA> V;
multiset<long long>R,B;
int cmp(PA a,PA b)
{
return a.first<b.first;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
long long x,y,ans;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>x>>y;
V.push_back(make_pair(min(x,y),max(x,y)));
R.insert(min(x,y));
B.insert(max(x,y));
}
long long maR=-INF,maB=-INF;
long long miR=INF,miB=INF;
sort(V.begin(),V.end(),cmp);
for(int i=0; i<V.size(); i++)
{
maR=max(V[i].first,maR);
maB=max(V[i].second,maB);
miR=min(V[i].first,miR);
miB=min(V[i].second,miB);
}
ans=(maR-miR)*(maB-miB);
for(int i=0; i<V.size(); i++)
{
R.erase(R.find(V[i].first));
B.insert(V[i].first);
B.erase(B.find(V[i].second));
R.insert(V[i].second);
ans=min(ans,(*R.rbegin()-*R.begin())*(*B.rbegin()-*B.begin()));
}
cout<<ans<<endl;
return 0;
}

/***********/

考虑所有数中的MAX和MIN,如果在Rmax=MAX&&Bmin=MIN,就需要让Rmin尽量大,Bmax尽量小,将每一对的大数染红,小数染黑;如果是Bmax=MAX&&Bmin=MIN,就需要让Rmax-Rmin尽量小,首先让所有xi<yi,按x排序,先把x全部染红,然后从小到大按顺序把x  y交换,计算交换后的Rmax和Rmin,更新一下Rmax-Rmin的最小值。因为如果存在xi<yi,而xi为红色,继续往后更新,和xi为黑色往后更新相比,最小值可能更小,而最大值不可能更小,因而不需要考虑这种情况。注意当MAX和MIN在同一对里时,不需要考虑后一种情况。

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=2e5+50;
 5 struct record
 6 {
 7     ll x,y;
 8 }stu[maxn];
 9 bool cmp(record p,record q) {return p.x<q.x;}
10 int n;
11 ll rmax,rmin,bmax,bmin,ma,mi,tree_max[maxn*4],tree_min[maxn*4];
12 void build(int l,int r,int rt){
13     if(l==r){
14         tree_max[rt]=stu[l].x;
15         tree_min[rt]=stu[l].x;
16         return ;
17     }
18     int mid=(l+r)/2;
19     build(l,mid,2*rt);
20     build(mid+1,r,2*rt+1);
21     tree_max[rt]=max(tree_max[2*rt],tree_max[2*rt+1]);
22     tree_min[rt]=min(tree_min[2*rt],tree_min[2*rt+1]);
23 }
24 void update(int pos,int l,int r,int rt)
25 {
26     if(l==r){
27         tree_max[rt]=stu[l].x;
28         tree_min[rt]=stu[l].x;
29         return ;
30     }
31     int mid=(l+r)/2;
32     if(pos<=mid) update(pos,l,mid,2*rt);
33     else update(pos,mid+1,r,2*rt+1);
34     tree_max[rt]=max(tree_max[2*rt],tree_max[2*rt+1]);
35     tree_min[rt]=min(tree_min[2*rt],tree_min[2*rt+1]);
36 }
37 int main()
38 {
39     scanf("%d",&n);
40     for(int i=1;i<=n;i++){
41         scanf("%lld %lld",&stu[i].x,&stu[i].y);
42         if(stu[i].x>stu[i].y) swap(stu[i].x,stu[i].y);
43         if(i==1){
44             ma=stu[i].y;
45             bmax=stu[i].y;
46             bmin=stu[i].y;
47             mi=stu[i].x;
48             rmax=stu[i].x;
49             rmin=stu[i].x;
50         }
51         else{
52             ma=max(stu[i].y,ma);
53             bmax=max(stu[i].y,bmax);
54             bmin=min(stu[i].y,bmin);
55             mi=min(mi,stu[i].x);
56             rmax=max(stu[i].x,rmax);
57             rmin=min(stu[i].x,rmin);
58         }
59     }
60     for(int i=1;i<=n;i++){
61         if(stu[i].x==mi && stu[i].y==ma){
62             printf("%lld\n",(rmax-rmin)*(bmax-bmin));
63             return 0;
64         }
65     }
66     sort(stu+1,stu+n+1,cmp);
67     ll sum=rmax-rmin,ans=(rmax-rmin)*(bmax-bmin);
68     build(1,n,1);
69     for(int i=1;i<=n;i++){
70         swap(stu[i].x,stu[i].y);
71         update(i,1,n,1);
72         rmax=tree_max[1];
73         rmin=tree_min[1];
74         sum=min(sum,rmax-rmin);
75     }
76     ans=min(ans,sum*(ma-mi));
77     printf("%lld\n",ans);
78     return 0;
79 }
View Code

 

posted @ 2018-06-13 18:20  Gang_Li  阅读(166)  评论(0编辑  收藏  举报