问题
https://vjudge.net/problem/UVA-1511
分析
两个点(x1,y1)和(x2,y2),现在x1<x2,y1和y2之间的关系未知。
∣
x
1
−
x
2
∣
+
∣
y
1
−
y
2
∣
=
x
2
−
x
1
+
∣
y
1
−
y
2
∣
|x1-x2|+|y1-y2|=x2-x1+|y1-y2|
∣x1−x2∣+∣y1−y2∣=x2−x1+∣y1−y2∣,当
y
1
<
y
2
时
,
x
2
−
x
1
+
y
2
−
y
1
=
−
(
x
1
+
y
1
)
+
(
x
2
+
y
2
)
y1<y2时,x2-x1+y2-y1=-(x1+y1)+(x2+y2)
y1<y2时,x2−x1+y2−y1=−(x1+y1)+(x2+y2)
当
y
1
>
y
2
时
,
x
2
−
x
1
+
y
1
−
y
2
=
(
−
x
1
+
y
1
)
+
(
x
2
−
y
2
)
y1>y2时,x2-x1+y1-y2=(-x1+y1)+(x2-y2)
y1>y2时,x2−x1+y1−y2=(−x1+y1)+(x2−y2)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100000+5,Inf=0x3f3f3f3f;
struct Point{
int x,y,type;
}point[2*maxn];
bool cmp1(const Point &lhs,const Point &rhs){
return lhs.y<rhs.y;
}
//
//bool cmp2(const Point &lhs,const Point &rhs){
// return lhs.y>rhs.y;
//}
int n,m;
int s,kase=0,a[maxn],b[maxn];
int main(void){
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d%d",&point[i].x,&point[i].y);
point[i].type=0;
}
scanf("%d",&m);
for(int i=n;i<n+m;++i){
scanf("%d%d",&point[i].x,&point[i].y);
point[i].type=1;
}
sort(point,point+n+m,cmp1);
int ans=Inf,low1=Inf,low2=Inf;
for(int i=0;i<n+m;++i){
if(point[i].type==1){
ans=min(ans,point[i].x+point[i].y+low1);
low2=min(low2,point[i].x-point[i].y);
}else{
ans=min(ans,-point[i].x+point[i].y+low2);
low1=min(low1,-point[i].x-point[i].y);
}
}
printf("%d\n",ans);
}
return 0;
}