Ball Coloring
6552: Ball Coloring
时间限制: 1 Sec 内存限制: 128 MB
提交: 13 解决: 7
[提交][状态][讨论版][命题人:admin]
题目描述
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
输入
N
x1 y1
x2 y2
:
xN yN
输出
样例输入
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的更多相关文章
- 【arc073e】Ball Coloring(线段树,贪心)
[arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...
- ARC 73 E - Ball Coloring
E - Ball Coloring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Ther ...
- ARC073E Ball Coloring
Problem AtCoder Solution 把点映射至二维平面,问题就变成了给定 \(n\) 个点,可以把点对 \(y=x\) 对称,求覆盖所有点的最小矩形面积. 可以先把所有点放到 \(y=x ...
- [AT2557] [arc073_c] Ball Coloring
题目链接 AtCoder:https://arc073.contest.atcoder.jp/tasks/arc073_c 洛谷:https://www.luogu.org/problemnew/sh ...
- AtCoder Regular Contest 073 E:Ball Coloring
题目传送门:https://arc073.contest.atcoder.jp/tasks/arc073_c 题目翻译 给你\(N\)个袋子,每个袋子里有俩白球,白球上写了数字.对于每一个袋子,你需要 ...
- AtCoder瞎做第二弹
ARC 067 F - Yakiniku Restaurants 题意 \(n\) 家饭店,\(m\) 张餐票,第 \(i\) 家和第 \(i+1\) 家饭店之间的距离是 \(A_i\) ,在第 \( ...
- 【AtCoder】ARC073
ARC 073 C - Sentou 直接线段覆盖即可 #include <bits/stdc++.h> #define fi first #define se second #defin ...
- AtCoder Regular Contest
一句话题解 因为上篇AGC的写的有点长……估计这篇也短不了所以放个一句话题解方便查阅啥的吧QwQ 具体的题意代码题解还是往下翻…… ARC 058 D:简单容斥计数. E:用二进制表示放的数字,然后状 ...
- AtCoder刷题记录
构造题都是神仙题 /kk ARC066C Addition and Subtraction Hard 首先要发现两个性质: 加号右边不会有括号:显然,有括号也可以被删去,答案不变. \(op_i\)和 ...
随机推荐
- iOS ipa包重签名
背景:公司做游戏SDK的,提供SDK给第三方后,他们打包过来我们需要分发在不同的渠道,这个时候需要修改SDK的配置文件,ipa文件修改后是需要手机越狱或者ipa重签名才能安装成功的,所以研究了一下重签 ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- MySQL查看版本号的五种方式介绍1111111
MySQL查看版本号的五种方式介绍 1 命令行模式登录MySQL [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the ...
- linux文件重命名
rename 命令用字符串替换的方式批量改变文件名. 语法 rename(参数) 参数 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标字符串: 文件:指定要改变文件 ...
- 3DMAX 多维材质及对应的UVW展开,UVW贴图
多维材质说明 多维材质就是一个模型多个材质,(混合材质是多个材质混一起,跟这个貌似没关,比如地表草地,泥土等的混合操作) 作用: 比如一个模型就是需要两种材质,刀的金属刀身,木质刀柄,墙的一面是木板, ...
- Linux上安装Apache服务器
http://httpd.apache.org/download.cgi httpd-2.4.29.tar.gz #创建httpd用户 groupadd httpd useradd -g httpd ...
- 使用Unity容器实现属性注入
简介 Unity :是微软用C#实现的轻量级,可扩展的依赖注入容器. 控制反转:(Inversion of Control,缩写为IoC),是用来消减程序之间的耦合问题,把程序中上层对下层依赖,转移到 ...
- CC08:翻转子串
题目 假定我们都知道非常高效的算法来检查一个单词是否为其他字符串的子串.请将这个算法编写成一个函数,给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次检查子串的函数. 给 ...
- Oracle更新表字段时内容中含有特殊字符&的解决方法
今天在做 Oracle表字段更新时出现了特殊字符&,导致无法更新. 这个问题是第二次碰到了,所以在此记录下,以备后用. 举例: update t set col1='A&B' wher ...
- C# 连接oracle,用32位client和64位Client,可能导致结果不同
在调用过程[pro_regentinitauth]时,有参数3-6为number类型,当我们用这样调用时,在32位client下,能正确得到输出参数3-6的结果为:1023, 但是,当我们把程序部署到 ...