题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6695
题意:给你n个人, 每个人都且仅只能表演一个节目, 一共两种节目, 每种节目造成的价值为表演这种
节目的最大值, 问怎么分配两种节目的价值差的绝对值最小。
先给x从大到小排序,枚举x为第一种节目的最大值,当前的x都比以前的x小,所以之前的y都是第二种节目
先找到x之前的y的最大值,更新答案,然后找到x之后并且和x最接近的y,如果y比之前的最大值大那么更新答案
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
struct node{
ll a, b;
}pos[maxn];
bool cmp(node a, node b){
return a.a > b.a;
}
multiset<ll> s;
int main()
{
int T, n;
scanf("%d",&T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%lld %lld", &pos[i].a, &pos[i].b), s.insert(pos[i].b);
}
sort(pos, pos + n, cmp);
multiset <ll> :: iterator it;
ll ans = 1e18+10, b = -1;
for(int i = 0; i < n; i++) {
s.erase(s.find(pos[i].b));
ll p = pos[i].a, p1, p2;
if(b != -1) {
ans = min(ans, abs(b - p));
}
if(!s.empty()) {
it = s.lower_bound(p);
if(it == s.end()) {
it--;
}
p1 = p2 = *it;
if(it != s.begin()){
it--;
p2 = *it;
}
}
if(b > p1) p1 = b;
if(b > p2) p2 = b;
ans = min(ans, min(abs(p1 - p), abs(p2 - p)));
b = max(b, pos[i].b);
}
printf("%lld\n", ans);
s.clear();
}
return 0;
}