Ball Coloring

球涂色问题解析
针对一个涉及N个袋子,每个袋子包含两个写有整数的白色球的问题,探讨了如何通过将球涂成红色或蓝色来最小化特定值(Rmax−Rmin)×(Bmax−Bmin)的方法。提供了两种解题思路及AC代码。

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

 

转载于:https://www.cnblogs.com/lglh/p/9179238.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值