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.

思维题,解法:

考虑所有数中最大的和最小的:MIN,MAXMIN,MAX,它们要么在两个集合中,要么只在一个集合中。不失一般性,我们考虑以下两种情况: 
   
  1.Rmin=MIN,Bmax=MAXRmin=MIN,Bmax=MAX 
  这时,我们要最小化RmaxRmax,最大化BminBmin,那么就只要在分组时把小的分给RR,大的分给BB即可。 
   
  2.Rmin=MIN,Rmax=MAXRmin=MIN,Rmax=MAX 
  这时,只需最小化Bmax−BminBmax−Bmin。这样的话我们知道肯定是要取,比如说2n2n个数一起排完序后,中间连续互斥的nn个。这样的话,我们先让每组中的xi<yixi<yi,然后按照xx数组排序,先让BB数组取x1∼xnx1∼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代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+;
struct record
{
ll x,y;
}stu[maxn];
bool cmp(record p,record q) {return p.x<q.x;}
int n;
ll rmax,rmin,bmax,bmin,ma,mi,tree_max[maxn*],tree_min[maxn*];
void build(int l,int r,int rt){
if(l==r){
tree_max[rt]=stu[l].x;
tree_min[rt]=stu[l].x;
return ;
}
int mid=(l+r)/;
build(l,mid,*rt);
build(mid+,r,*rt+);
tree_max[rt]=max(tree_max[*rt],tree_max[*rt+]);
tree_min[rt]=min(tree_min[*rt],tree_min[*rt+]);
}
void update(int pos,int l,int r,int rt)
{
if(l==r){
tree_max[rt]=stu[l].x;
tree_min[rt]=stu[l].x;
return ;
}
int mid=(l+r)/;
if(pos<=mid) update(pos,l,mid,*rt);
else update(pos,mid+,r,*rt+);
tree_max[rt]=max(tree_max[*rt],tree_max[*rt+]);
tree_min[rt]=min(tree_min[*rt],tree_min[*rt+]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld %lld",&stu[i].x,&stu[i].y);
if(stu[i].x>stu[i].y) swap(stu[i].x,stu[i].y);
if(i==){
ma=stu[i].y;
bmax=stu[i].y;
bmin=stu[i].y;
mi=stu[i].x;
rmax=stu[i].x;
rmin=stu[i].x;
}
else{
ma=max(stu[i].y,ma);
bmax=max(stu[i].y,bmax);
bmin=min(stu[i].y,bmin);
mi=min(mi,stu[i].x);
rmax=max(stu[i].x,rmax);
rmin=min(stu[i].x,rmin);
}
}
for(int i=;i<=n;i++){
if(stu[i].x==mi && stu[i].y==ma){
printf("%lld\n",(rmax-rmin)*(bmax-bmin));
return ;
}
}
sort(stu+,stu+n+,cmp);
ll sum=rmax-rmin,ans=(rmax-rmin)*(bmax-bmin);
build(,n,);
for(int i=;i<=n;i++){
swap(stu[i].x,stu[i].y);
update(i,,n,);
rmax=tree_max[];
rmin=tree_min[];
sum=min(sum,rmax-rmin);
}
ans=min(ans,sum*(ma-mi));
printf("%lld\n",ans);
return ;
}

Ball Coloring的更多相关文章

  1. 【arc073e】Ball Coloring(线段树,贪心)

    [arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...

  2. ARC 73 E - Ball Coloring

    E - Ball Coloring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Ther ...

  3. ARC073E Ball Coloring

    Problem AtCoder Solution 把点映射至二维平面,问题就变成了给定 \(n\) 个点,可以把点对 \(y=x\) 对称,求覆盖所有点的最小矩形面积. 可以先把所有点放到 \(y=x ...

  4. [AT2557] [arc073_c] Ball Coloring

    题目链接 AtCoder:https://arc073.contest.atcoder.jp/tasks/arc073_c 洛谷:https://www.luogu.org/problemnew/sh ...

  5. AtCoder Regular Contest 073 E:Ball Coloring

    题目传送门:https://arc073.contest.atcoder.jp/tasks/arc073_c 题目翻译 给你\(N\)个袋子,每个袋子里有俩白球,白球上写了数字.对于每一个袋子,你需要 ...

  6. AtCoder瞎做第二弹

    ARC 067 F - Yakiniku Restaurants 题意 \(n\) 家饭店,\(m\) 张餐票,第 \(i\) 家和第 \(i+1\) 家饭店之间的距离是 \(A_i\) ,在第 \( ...

  7. 【AtCoder】ARC073

    ARC 073 C - Sentou 直接线段覆盖即可 #include <bits/stdc++.h> #define fi first #define se second #defin ...

  8. AtCoder Regular Contest

    一句话题解 因为上篇AGC的写的有点长……估计这篇也短不了所以放个一句话题解方便查阅啥的吧QwQ 具体的题意代码题解还是往下翻…… ARC 058 D:简单容斥计数. E:用二进制表示放的数字,然后状 ...

  9. AtCoder刷题记录

    构造题都是神仙题 /kk ARC066C Addition and Subtraction Hard 首先要发现两个性质: 加号右边不会有括号:显然,有括号也可以被删去,答案不变. \(op_i\)和 ...

随机推荐

  1. Razor的主版页面框架

    类似于2.0版本中的MasterPage主版页面框架,不过mvc3.0推出的RazorView内建的主版页面语法与原本的webFormview的MasterPage相差甚远   1,Razor的页面执 ...

  2. To the max(求最大子矩阵和)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47985   Accepted: 25387 Desc ...

  3. python 之 time模块、datetime模块(打印进度条)

    6.9 time 模块 方法 含义 备注 time.time() 时间戳 1561013092.997079 time.strftime('%Y-%m-%d %H:%M:%S %p') 结构化时间st ...

  4. P5346 【XR-1】柯南家族

    题目地址:P5346 [XR-1]柯南家族 Q:官方题解会咕么? A:不会!(大雾 题解环节 首先,我们假设已经求出了 \(n\) 个人聪明程度的排名. \(op = 1\) 是可以 \(O(1)\) ...

  5. Django-Rest-Framework的序列化之serializers 序列化组件

    Django-Rest-Framework的序列化之serializers 序列化组件 restful framework 正常的序列化 from django.http import HttpRes ...

  6. CodeForces - 1004A-Sonya and Hotels(思维)

    Sonya decided that having her own hotel business is the best way of earning money because she can pr ...

  7. POJ3178 计算几何+DP

    //一些点一些圆,过圆不能连线,相邻点不能连线,问最多连几条线 //计算几何模板+区间dp //关键是判断圆和线段是否相交 #include <cstdio> #include <c ...

  8. Linux上常用命令整理(一)—— cat

    近几个月刚从windows上转过来,开始慢慢熟悉linux,先不撕比到底哪个更好,首先要怀着相互借鉴的精神去了解各个平台. Linux上做开发,除去使用文本编辑器做开发的大神之外,大家(包括我这种菜鸟 ...

  9. winform 程序隐藏窗口运行

    DWPublishForm frm = new DWPublishForm(); frm.IsAutoUpdate = true; frm.ShowInTaskbar = false; frm.For ...

  10. Column 'xxx' in field list is ambiguous

    一 其实看一下ambiguous的翻译就好了 一开始我觉得是含糊什么的,后来找了下才知道应该是双关... 二 所以翻译过来就是 : 列'XX'在字段列表中双关 其实就是两张表有相同的字段,但是使用时, ...