Welcome Party

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 875    Accepted Submission(s): 194

Problem Description
The annual welcome party of the Department of Computer Science and Technology is coming soon! Many students have been applying to show up at the welcome party, and every one of them can choose to sing a song or play crosstalk. This troubles the chief director a lot: how to arrange the program list, such that every student can have a chance to show up on the stage, and the satisfactory value of audiences is maximized?

To cope with this problem, the director proposes a model. In this model, every student has two attributes: the singing ability and crosstalking ability. The satisfactory value of audiences to singings is the maximum singing ability among all students that choose to sing a song; similarly, the satisfactory value to crosstalks is the maximum crosstalking ability among all students that choose play crosstalk. The strange thing is, the overall satisfactory value to the whole party is negatively related to the absolute difference between the satisfactory values to singings and crosstalks. The problem is, what is the minimum possible absolute difference between the satisfactory values of the two types of programs?

Note that:
- every student should choose exactly one type of programs to play;
- at least one student should sing a song, and at least one student should play crosstalk.

 
Input
The first line of input consists of a single integer T (1≤T≤70), the number of test cases.

Each test case starts with a line of a single integer n (2≤n≤100000), denoting the number of students applying to show up on the stage. Then follow n lines, each containing two integers x and y (0≤x,y≤1018), denoting the singing ability and crosstalking ability of a student.

It is guaranteed that the sum of n over all test cases never exceeds 1000000.

 
Output
For each test case, output a single integer, denoting the minimum possible absolute difference between the satisfactory values of the two types of programs.
 
Sample Input
2
5
27 46
89 13
55 8
71 86
22 35
3
3 5
4 7
6 2
 
Sample Output
3
1
 
Source
 

题意:

有n个学生,每个学生有x值和y值,每个学生必须要么进入x集合要么y集合,x集合的代表值是这个集合中最大的x值,y集合同理,问如何分配学生才能使得x集合代表值与y集合的代表值差值最小

思路:

n最大1e5,所以复杂度最大只能为O(nlogn),可以考虑遍历x集合的同时二分y集合
用一个结构体存学生的x值和y值,并对对学生按x和y降序排序,将学生的y值加入到multiset中
因为现在是降序排序,所以如果选了一个学生代表x集合的代表,则x比他大的学生都必须要加入到y集合中,而比小于等于他的那些则可以随意
所以我们枚举每个学生当x集合的代表,并将其在y集合中的值删掉,用maxn维护必须要加入y集合的那些值的最大值,multiset维护可能可以加入y集合的值,并multiset中二分一个和他最接近的值(可能大于,等于或小于,所以如果it!=s.end()还有it--判断),
当然这个值要比()才能更新ans,再用maxn和现在这个学生x值的差值更新ans,这个学生判断完后就去判断下一个学生,当然因为我们对他们x值降序排序了,所以这个学生就必须加入y集合,用其y值更新maxn

注意(坑点,WA了几十次TAT):

1.maxn代表必定要分到另一个集合的元素的最大值,而第一次没有必须要分到另一个集合中的元素,所以第一次是不能调用的

2.因为一个集合的代表值是这个集合中所以元素中的最大值,所以二分到的值必须要大于maxn才能更新ans

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
struct node{
ll x,y;
}a[amn];
multiset<ll> s;
multiset<ll>::iterator it;
bool cmp(node a,node b){
if(a.x==b.x)return a.y>b.y;
return a.x>b.x;
}
int main(){
int n,T;
bool f;
ll ans,maxn;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld%lld",&a[i].x,&a[i].y);
s.insert(a[i].y); ///将y值插入multiset
}
sort(a+,a++n,cmp); ///对学生按x降序排序
ans=1e18;
maxn=; ///maxn维护一个必须要加入y集合的学生中y值最大的值
for(int i=;i<=n;i++){
s.erase(s.find(a[i].y)); ///因为现在把当前学生分到x集合中,所以删除当前学生在multiset中的y值
if(i>)ans=min(ans,abs(a[i].x-maxn)); ///这里是关键,maxn代表必定要分到另一个集合的元素的最大值,而第一次没有必须要分到另一个集合中的元素,所以第一次是不能调用的,否则因为一开始maxn为0,所以第一次会将ans更新为a[1].x
it=s.lower_bound(a[i].x); ///在multiset中二分一个和当前学生x值最接近的值
if(it!=s.begin()&&it==s.end())it--; ///lower_bound会返回第一个大于等于搜索值的地址,如果不存在则返回s.end(),所以如果迭代器不是s.begin()且是s.end()则要将其指向上一个元素
if(*it>=maxn)ans=min(ans,abs(a[i].x-*it)); ///因为一个集合的代表值是这个集合中所以元素中的最大值,所以二分到的值必须要大于maxn才能更新ans
if(it!=s.begin()){ ///如果前面还有元素,则看上一个元素是否符合条件
it--;
if(*it>=maxn)ans=min(ans,abs(a[i].x-*it));
}
maxn=max(maxn,a[i].y); ///现在这个学生必须要被加到y集合中了,更新一下maxn
}
printf("%lld\n",ans);
}
}
/**
有n个学生,每个学生有x值和y值,每个学生必须要么进入x集合要么y集合,x集合的代表值是这个集合中最大的x值,y集合同理,问如何分配学生才能使得x集合代表值与y集合的代表值差值最小
n最大1e5,所以复杂度最大只能为O(nlogn),可以考虑遍历x集合的同时二分y集合
用一个结构体存学生的x值和y值,并对对学生按x和y降序排序,将学生的y值加入到multiset中
因为现在是降序排序,所以如果选了一个学生代表x集合的代表,则x比他大的学生都必须要加入到y集合中,而比小于等于他的那些则可以随意
所以我们枚举每个学生当x集合的代表,并将其在y集合中的值删掉,用maxn维护必须要加入y集合的那些值的最大值,multiset维护可能可以加入y集合的值,并multiset中二分一个和他最接近的值(可能大于,等于或小于,所以如果it!=s.end()还有it--判断),
当然这个值要比()才能更新ans,再用maxn和现在这个学生x值的差值更新ans,这个学生判断完后就去判断下一个学生,当然因为我们对他们x值降序排序了,所以这个学生就必须加入y集合,用其y值更新maxn
注意(坑点):
1.maxn代表必定要分到另一个集合的元素,而第一次没有必须要分到另一个集合中的元素,所以第一次是不能调用的
2.因为一个集合的代表值是这个集合中所以元素中的最大值,所以二分到的值必须要大于maxn才能更新ans
**/

[二分,multiset] 2019 Multi-University Training Contest 10 Welcome Party的更多相关文章

  1. 2016 Multi-University Training Contest 10

    solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...

  2. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

  3. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  5. 2015 Multi-University Training Contest 10(9/11)

    2015 Multi-University Training Contest 10 5406 CRB and Apple 1.排序之后费用流 spfa用stack才能过 //#pragma GCC o ...

  6. 2019 Multi-University Training Contest 10

    目录 Contest Info Solutions C - Valentine's Day D - Play Games with Rounddog E - Welcome Party G - Clo ...

  7. 【2019 Multi-University Training Contest 10】

    01: 02: 03:https://www.cnblogs.com/myx12345/p/11671692.html 04: 05:https://www.cnblogs.com/myx12345/ ...

  8. [概率] HDU 2019 Multi-University Training Contest 10 - Valentine's Day

    Valentine's Day Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others ...

  9. [dfs] HDU 2019 Multi-University Training Contest 10 - Block Breaker

    Block Breaker Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)T ...

随机推荐

  1. Cisco2960 交换机密码破解方法

    1. 启用超级终端并出现连接界面2. 拔掉交换机的电源3. 按下交换机上的 Mode按钮, 与此同时重新插上交换机的电源线4. 当交换机左边的sys 灯会慢慢的闪动, ( 细心的话会发现部分灯红色的闪 ...

  2. 先搞清楚这些问题,简历上再写你熟悉Java!

    原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 系列文章介绍 本文是<五分钟学Java>系列文章的一篇 本系列文章主要围绕Java程序员必须掌握的核心技能,结合我个人三年 ...

  3. Python 将两个绝对路径拼接

    import os def two_abs_join(abs1, abs2): """ 将 绝对路径将两个绝对路径拼接, 就是将第二个的开路径(windows 的 C, ...

  4. 挖SRC逻辑漏洞心得分享

    文章来源i春秋 白帽子挖洞的道路还漫长的很,老司机岂非一日一年能炼成的. 本文多处引用了 YSRC 的 公(qi)开(yin)漏(ji)洞(qiao).挖SRC思路一定要广!!!!漏洞不会仅限于SQL ...

  5. Flutter保持页面状态AutomaticKeepAliveClientMixin

    使用bottomNavigationBar切换底部tab,再切换回来就会丢失之前的状态(重新渲染列表,丢失滚动条位置). 解决方法 使用 AutomaticKeepAliveClientMixin 重 ...

  6. git的日常使用(补课)

    使用git的一些反响 如果在github上做一个仓库来使用的话... 首先手动在github上创建一个空的仓库,默认没有任何东西的 使用git的命令 git pull 创建的github仓库地址 能把 ...

  7. Python卸载

    前言 自己瞎折腾下载Python3.8.2,把之前下载好的python3.7.3覆盖掉.在运行之前Python环境的程序多次未果后.找到原因,Python3.7.3的包不支持Python3.8.2.于 ...

  8. Java实现GBK转码到UTF-8(文件)

    所以,虚无的悲叹,寻根问底仍是由于肉身的圈定.肉身蒙蔽了灵魂的眼睛,单是看见要回那无中去,却忘了你原是从那无中来.                                            ...

  9. 吐血干货,直播首屏耗时400ms以下的优化实践

    导读: 直播行业的竞争越来越激烈,进过18年这波洗牌后,已经度过了蛮荒暴力期,剩下的都是在不断追求体验.最近在帮做直播优化首开,通过多种方案并行,把首开降到500ms以下,希望能对大家有借鉴. 背景: ...

  10. mybatis探究之延迟加载和缓存

    mybatis探究之延迟加载和缓存 一.什么是延迟加载 1.延迟加载的概念 在mybatis进行多表查询时,并非所有的查询都需要立即进行.例如在查询带有账户信息的用户信息时,我们们并不需要总是在加载用 ...