标签:style http color os io for ar line
题目大意:给出两个点集,问说分别从两个点集中取一点的哈夫曼距离最小值。注意一个点集的x坐标小于0,另一个大于0.
解题思路:因为x2一定大于x1,所以对于x这一维,一定是+x2-x1,所以只需要考虑y这一维坐标即可。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
struct point {
int x, y, flag;
}p[maxn*2];
int N, M;
bool cmp (const point& a, const point& b) {
return a.y < b.y;
}
void init () {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].flag = 0;
}
scanf("%d", &M);
for (int i = 0; i < M; i++) {
scanf("%d%d", &p[i+N].x, &p[i+N].y);
p[i+N].flag = 1;
}
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
int ans = INF, now = INF;
sort(p, p + N + M, cmp);
for (int i = 0; i < N + M; i++) {
if (p[i].flag)
now = min(now, p[i].x - p[i].y);
else
ans = min(ans, now - p[i].x + p[i].y);
}
now = INF;
reverse(p, p + N + M);
for (int i = 0; i < N + M; i++) {
if (p[i].flag)
now = min(now, p[i].x + p[i].y);
else
ans = min(ans, now - p[i].x - p[i].y);
}
printf("%d\n", ans);
}
return 0;
}
uva 1511 - Soju(贪心),布布扣,bubuko.com
标签:style http color os io for ar line
原文地址:http://blog.csdn.net/keshuai19940722/article/details/38145419