2019 杭电多校 10 1005

题目链接:HDU 6695

比赛链接:2019 Multi-University Training Contest 10

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\le T\le 70)\), the number of test cases.

Each test case starts with a line of a single integer \(n (2\le n\le 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\le x,y\le 10^{18})\), 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

Solution

题意

有 \(n\) 个人,\(2\) 种节目,每个人要表演其中的一种节目,每种节目至少有一人表演。用 \(x_i\) 和 \(y_i\) 表示第 \(i\ (1\le i\le n)\) 个人表演两种节目的能力值。现在要使表演第一种节目的人中的能力最大值与表演第二种节目的人中的能力最大值之差最小,求这个最小值。

题解

贪心

如下图,维护两个集合 \(s_1\) 和 \(s_2\)。

按 \(x\) 从大到小枚举。假设 \(x_i\) 为 \(x\) 中的最大值 (下图中的 \(4\)),则比 \(x_i\) 大的都选择 \(y\),也就是取 \(s_1\) 中的最大值 (下图中的 \(8\))。比 \(x_i\) 小的取与 \(x_i\) 最接近的 \(y\) (下图中的 \(3\)),因为更大的 \(y\) 可以选择 \(x\) (下图中的 \(7\) 可以用 \(2\) 替换)。然后取两个的较大值更新到 \(ans\) (下图中 \(|3 - 4| < |8 - 4|\) 取 \(8 - 4\)),维护最小值 \(ans\) 即可。

比赛中队友 (线段树大佬) 用线段树过的。赛后我用 \(multiset\) 写了一下。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 5;
const ll inf = 2e18; struct STU {
ll x, y;
} s[maxn];
ll maxy[maxn]; int cmp(STU s1, STU s2) {
return s1.x > s2.x;
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
int n;
cin >> n;
multiset<ll> s1, s2;
for(int i = 0; i < n; ++i) {
cin >> s[i].x >> s[i].y;
s2.insert(s[i].y);
}
sort(s, s + n, cmp);
ll ans = inf;
for(int i = 0; i < n; ++i) {
s2.erase(s2.find(s[i].y)); // 一个人只能选择一种表演
if(!s1.empty()) {
ans = min(ans, abs(*s1.rbegin() - s[i].x));
}
if(!s2.empty()) {
multiset<ll>::iterator it = s2.lower_bound(s[i].x); // 找到第一个大于等于 s[i].x 的 y
if(it == s2.end()) {
--it;
}
ll tmp = abs(*it - s[i].x);
if(tmp < ans && (s1.empty() || *it > *s1.rbegin())) {
ans = tmp;
}
if(it != s2.begin()) { // 找到最后一个小于 s[i].x 的 y
--it;
tmp = abs(*it - s[i].x);
if(tmp < ans && (s1.empty() || *it > *s1.rbegin())) {
ans = tmp;
}
}
s1.insert(s[i].y);
}
}
cout << ans << endl;
}
return 0;
}

HDU 6695 Welcome Party (贪心)的更多相关文章

  1. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  2. HDU 5835 Danganronpa (贪心)

    Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...

  3. HDU 5821 Ball (贪心)

    Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

  4. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  5. Saving HDU(hdu2111,贪心)

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. HDU 4714 Tree2cycle:贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...

  7. HDU 5303 Delicious Apples (贪心 枚举 好题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)

    Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. hdu 5037 Frog(贪心)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5037 题解:为了让放的石头有意义肯定是没l+1的距离放2个也就是说假设现在位置为pos那么 ...

随机推荐

  1. java并发编程笔记(四)——安全发布对象

    java并发编程笔记(四)--安全发布对象 发布对象 使一个对象能够被当前范围之外的代码所使用 对象逸出 一种错误的发布.当一个对象还没构造完成时,就使它被其他线程所见 不安全的发布对象 某一个类的构 ...

  2. PAT 1036 Boys vs Girls (25 分)

    1036 Boys vs Girls (25 分)   This time you are asked to tell the difference between the lowest grade ...

  3. LInux终端中Ctrl+S卡死

    因为初学Linux,在vim中写东西是总是喜欢按Ctrl+s来保存内容导致终端突然卡主,然后上网查资料发现了Ctrl+s 暂停屏幕输出[锁住终端]而对应的按键是Ctrl+q 恢复屏幕输出[解锁终端]

  4. Cococs2d-x学习路线

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. Cocos2D-X推荐书: Cocos2d-x权威指南

  5. 使用正则限制input框只能输入数字/英文/中文等等

    常用HTML正则表达式 1.只能输入数字和英文的: 复制代码代码如下: <input onkeyup="value=value.replace(/[/W]/g,'') " o ...

  6. 【最新】docker 安装elasticsearch + kibana步骤【第二篇_kibana】

    本文主要讲解Docker 安装 kibana并设置中文语言 [如果有需要安装elasticsearch 的朋友请移步博主第一篇文章] 话不多说! 第一步:docker 下载kibana docker ...

  7. Feign实现服务调用

    上一篇博客我们使用ribbon+restTemplate实现负载均衡调用服务,接下来我们使用feign实现服务的调用,首先feign和ribbon的区别是什么呢? ribbon根据特定算法,从服务列表 ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String

    题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...

  9. day11 python名称空间 作用域

    day11 python   一.三元运算符 def func(a, b):     return a if a > b else b   print(func(44,66))     二:函数 ...

  10. Java中面向对象三大特性之继承

    1. 继承的概述 继承就是子类继承父类的变量和方法,下面用代码解释一下: class Student {// 定义学生类 String name; int age; void study() { Sy ...