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. 938. Range Sum of BST

    Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...

  2. 51nod1241(连续上升子序列)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1241 题意:中文题诶- 思路:通过观察我们不难发现就是找连续 ...

  3. codevs1251 括号

    1251 括号  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 计算乘法时,我们可以添加括号,来改变相乘的顺序,比 ...

  4. Netty源码分析(七):初识ChannelPipeline

    ChannelPipeline单看名称就可以知道Channel的管道.本篇将结合它的默认实现类DefaultChannelPipeline来对它做一个简单的介绍. 示例图 上图是官方提供的Channe ...

  5. 洛谷 P1070 道路游戏(noip 2009 普及组 第四题)

    题目描述 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有 nn个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针顺序依次将这 nn个机器人工厂编 ...

  6. nginx反向代理解决跨域问题,使本地调试更方便

    我们可能都会遇到一个这样的问题,线上环境是https://...,本地启动了项目,域名是localhost:8000等,本地想要访问线上的接口,直接在本地调试,却提示跨域,这个时候我们可以配置ngin ...

  7. Jmeter集成Jira提交缺陷

    笔者曾在文章<Jmeter排忧解难—生成excel结果文件>聊到了一种提高接口测试效率的方法.今天,咱们接着对“提高接口测试效率”这个话题做更深入的探讨.作为一名接口测试人员,我们是否一直 ...

  8. js弹框怎么获得父页面的元素

    js获取父页面的元素可以用$(window.parent.document).find("#customer_id").val();这里的customer_id表示父页面某一个元素 ...

  9. 转 db_file_multiblock_read_count

    http://www.laoxiong.net/table_scan_and_buffer_cache.html 全表扫描与buffer cache https://www.cnblogs.com/R ...

  10. (转) Linux 下的dd命令使用详解(摘录)

    使用dd命令克隆整个系统------http://www.cnblogs.com/jikexianfeng/p/6103504.html 原文:https://www.cnblogs.com/jike ...