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. 我也来Show一下我的VisualStudio2017

    1.首先,在微软官方网站下载VS2017的安装程序,后续的安装将通过这个安装程序来引导.这里有三个版本可供选择:社区版.专业版和企业版,社区版免费,专业版和企业版可以免费体验,之后收费,当然,在中国盗 ...

  2. cf822C(贪心)

    题目链接: http://codeforces.com/problemset/problem/822/C 题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cost(1 ...

  3. DDD 落地的具体思路

    学习 DDD 的朋友有两种,一种是看 DDD 经典书籍 <领域驱动设计:软件核心复杂性应对之道>完全看不懂,第二种是看啥都懂,都觉得有道理,但总是落不了地. 我们总结一下我们自己落地的思路 ...

  4. 【NOI2012】迷失游乐园

    题目链接:迷失游乐园(BZOJ)  迷失游乐园(Luogu) 独立完成的题,写一发题解纪念一波~ 模拟完样例大概可以知道是道树形DP了. 观察数据范围,发现是基环树,至少会有一个环. 先从树的部分开始 ...

  5. ORA-01950:表空间“USERS”中无权限

    ORA-01950:表空间“USERS”中无权限 解决方案: A)确认给用户授权了resource角色 B)取消限制 ALTER USER "HCCPMS" QUOTA UNLIM ...

  6. LOJ 2288「THUWC 2017」大葱的神力

    LOJ 2288「THUWC 2017」大葱的神力 Link Solution 比较水的提交答案题了吧 第一个点爆搜 第二个点爆搜+剪枝,我的剪枝就是先算出 \(mx[i]\) 表示选取第 \(i \ ...

  7. CentOS,net core2 sdk nginx、supervisor、mysql

    CentOS下 .net core2 sdk nginx.supervisor.mysql环境搭建 作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把 ...

  8. response返回字符床

    response.getWriter().println() 本来一个html,JSP等WEB资源返回的就是一个String,只是有时候这个String是符合html格式的,而刚是浏览器接收的了,所以 ...

  9. [luogu 1967]货车运输

    货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情 ...

  10. 编译安装php容易出现的问题以及解决办法

    http://crybit.com/20-common-php-compilation-errors-and-fix-unix/