题意:
点分为两个区,一个区的横坐标小于第二个区,求abs(x1-x2)+abs(y1-y2)的最小值
思路:
主要思想就是把下标一样的同一到一边,那就要分类讨论
由于x1<x2所以就是x2-x1,然后分类讨论y1,y2的情况,所以循环两次,当然两边是需要同时维护的
代码:
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 100005;
const int INF = 0x3f3f3f3f;
int n, m;
struct point {
int x, y;
int flag;
}p[maxn*2];
int cmp(point a, point b) {
return a.y < b.y;
}
void deal() {
int temp = INF, ans = INF;//分两次讨论
for(int i=0; i<n+m; i++) {
if(p[i].flag) {//y2<y1
temp = min(temp, p[i].x-p[i].y);
}
else ans = min(ans, temp+p[i].y-p[i].x);
}
temp = INF;
for(int i=0; i<n+m; i++) {
if(!p[i].flag) {
temp = min(temp, -(p[i].x+p[i].y));
}
else {
ans = min(ans, temp+p[i].x+p[i].y);
}
}
printf("%d\n", ans);
}
int main() {
int kase;
scanf("%d", &kase);
while(kase--) {
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;
}
sort(p, p+n+m, cmp);
deal();
}
return 0;
}