Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 544 Accepted Submission(s): 103
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
2019 Multi-University Training Contest 10
Recommend
chendu | We have carefully selected several similar problems for you: 6701 6700 6699 6698 6697
Statistic | Submit | Discuss | Note
题意: n个人, 每个人都要表演一个节目, 一共两种节目, 每种节目造成的价值为表演这种
节目的最大值, 问怎么分配两种节目的价值差最小。
思路: 将给的n个人, 按照x从小到大排序, 从大到小枚举当前的x为最大值,
使用两个multiset s1, s2来记录y的值。 当前枚举的x之前的x都比其大, 所以
前面的人都会表演y节目, 所以s1 每次加入前面的人, s2记录后面的y值, 先
在s1中找到最大的值, 更新答案, 再在s2中找到最接近x的值, 若这个值大于
s1中最大值, 即可更新。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifdef LOCAL
#define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n"
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#else
#define TIME 0
#endif
#define continue(x) { x; continue; }
#define break(x) { x; break; }
ll mod = 1e9 + 7;
ll fpow(ll a, ll b) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; }
const int N = 1e6 + 10;
struct node
{
ll x, y;
bool operator < (const node & oth)const
{
return x > oth.x;
}
}a[N];
ll jdz(ll a)
{
return a < 0 ? -a : a;
}
int main()
{
#ifdef LOCAL
freopen("D:/input.txt", "r", stdin);
#endif
int t;
cin >> t;
while (t--)
{
multiset<ll>s1; // 前面的y值
multiset<ll>s2; // 后面的y值
int n;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i].x, &a[i].y), s2.insert(a[i].y);
sort(a + 1, a + n + 1);
ll ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 1; i <= n; i++) // 枚举x最大值
{
s2.erase(s2.find(a[i].y)); // 删除当前人, 使其目前表演第一个节目
if (!s1.empty()) // s1中的都是跟当前人不同节目的
{
ans = min(ans, jdz(*s1.rbegin() - a[i].x));
}
if (!s2.empty())
{
auto it = s2.lower_bound(a[i].x); // 查找最接近的值
if (it == s2.end())
--it;
ll nans = jdz(*it - a[i].x);
if (nans < ans && (s1.empty() || *it > *s1.rbegin())) // 只有当前这个值大于s1中最大值
ans = nans; // 即可以让其表演第二个节目成为y中的最大值
if (it != s2.begin())
{
--it;
nans = jdz(*it - a[i].x);
if (nans < ans && (s1.empty() || *it > *s1.rbegin()))
ans = nans;
}
}
s1.insert(a[i].y); // 加入当前的y, 成为表演第二个节目的人
}
cout << ans << endl;
}
return TIME;
}