题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6695
题解:分成两组,其中一组最大的x - 另一组最大的y 绝对值最小
题解:按照x从小到大排序,枚举最大x,比x大的都放到另一组,每处理完y记录一下,然后从前面找到大于x和小于x的最符合的,判断一下是否可取,然后就是很多x相等的,找一个y最符合的
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100100;
int n;
struct node {
ll x, y;
bool operator <(const node &b) const {
return x < b.x;
}
}a[N];
ll maxx[N];
int main() {
int T;
ll ans, cnt, cnt1, cnt2, tmp;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld %lld", &a[i].x, &a[i].y);
sort(a + 1, a + 1 + n);
maxx[n + 1] = 0;
for(int i = n; i >= 1; i--) maxx[i] = max(a[i].y, maxx[i + 1]);
set<ll> s;
set<ll>::iterator it;
ans = 1e18;
for(int i = 1, j = 1, k; i <= n;) {
cnt = maxx[i + 1];
tmp = abs(cnt - a[i].x);
cnt1 = -1;
cnt2 = -1;
it = s.lower_bound(a[i].x);
if(i != 1) {
if(it != s.begin()) {
it--;
cnt1 = *it;
}
}
it = s.lower_bound(a[i].x);
if(it != s.end()) {
cnt2 = *it;
}
if(cnt1 != -1 && cnt1 > cnt) tmp = min(tmp, abs(cnt1 - a[i].x));
if(cnt2 != -1 && cnt2 > cnt) tmp = min(tmp, abs(cnt2 - a[i].x));
j = i + 1;
s.insert(a[i].y);
if(i + 1 <= n && a[i].x == a[i + 1].x) {
k = i + 1;
while(k <= n && a[i].x == a[k].x) k++;
j = i;
while(j < k ) {
if(a[j].y >= maxx[k]) tmp = min(tmp, abs(a[i].x - a[j].y));
s.insert(a[j].y);
j++;
}
}
i = j;
ans = min(ans , tmp);
}
printf("%lld\n", ans);
}
return 0;
}