题解:P16862 [GKS 2021 #G] Staying Hydrated
tLiYueawa_ · · 题解
P16862 [GKS 2021 #G] Staying Hydrated
蒟蒻的第一篇题解
思路
我的核心思路和别人的相似,是利用曼哈顿距离的可分离性,将二维问题转化为两个独立的一维中位数问题。
可以发现,
实现
收集:把所有矩形的左/右
排序:对
取值:直接取排序后第
Code
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int xs[N],ys[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);//不加就TLE了
int T;
cin>>T;
for (int j = 1; j <= T; j++) {
int k;
cin >> k;
for (int i = 0; i < k; i++) {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
xs[2 * i] = x1;
xs[2 * i + 1] = x2;
ys[2 * i] = y1;
ys[2 * i + 1] = y2;
}
sort(xs,xs+2*k);
sort(ys,ys+2*k);
int x = xs[k - 1], y = ys[k - 1];
cout << "Case #" << j << ": " << x << " " << y << endl;
}
return 0;
}