题目传送门
Problem Description
The last trial Venus imposes on Psyche is a quest to the underworld. She is to take a box and obtain in it a dose of the beauty of Prosperina, queen of the underworld.
There are n palaces in the underworld, which can be located on a 2-Dimension plane with (x,y) coordinates (where x,y are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace.
However, the underworld is mysterious and changes all the time. At different times, exactly one of the n palaces disappears.
Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared.
Print the sum of the distance after every single palace has disappeared.
To avoid floating point error, define the distance d between palace (x1,y1) and (x2,y2) as d=(x1−x2)2+(y1−y2)2.
Input
The first line of the input contains an integer T (1≤T≤5), which denotes the number of testcases.
For each testcase, the first line contains an integers n (3≤n≤10^5), which denotes the number of temples in this testcase.
The following n lines contains n pairs of integers, the i-th pair (x,y) (−10^5 ≤x,y≤ 10^5) denotes the position of the i-th palace.
Output
For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.
Sample Input
1
3
0 0
1 1
2 2
Sample Output
12
思路
最近点对的小变形,因为最近点对距离改变只可能是删掉了这最近点对中的点。所以先跑一次最近点对,找出最近点对的距离并且记录是最近点对。所以有N-2次删除点是不会对答案造成影响的。所以可以直接ans * (n-2);然后分别枚举删除一次最近点对,把两个答案加入到ans中最后输出即可。时间复杂度大概是O(NlogN)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
const int inf = 1<<30;
struct point{
ll x,y;
}p[100005],t[100005],f[100005];
point na,nb,nc;
ll res;
bool cmpxy(point a,point b)
{
if(a.x != b.x){
return a.x < b.x;
}
else{
return a.y < b.y;
}
}
bool cmpy(point a,point b)
{
return a.y < b.y;
}
ll dist(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
ll solve(int left,int right)
{
if(left == right) return inf;
if(right - left == 1){
ll value = dist(p[left],p[right]);
if(value < res){ //更新最近点对和距离
na = p[left];nb = p[right];
res = value;
}
return value;
}
int m = (left + right) / 2;
ll d1 = solve(left,m);
ll d2 = solve(m+1,right);
ll d = min(d1,d2);
int k = 0;
for(int i = left;i <= right;i++){
if(abs(p[i].x - p[m].x) <= d){
t[k++] = p[i];
}
}
sort(t,t+k,cmpy);
for(int i = 0;i < k;i++){
for(int j = i+1;j < k;j++){
if(abs(t[j].y - t[i].y) >= d) break; //小剪枝
ll sum = dist(t[i],t[j]);
d = min(d,sum);
if(sum < res){ //更新最近点对和距离
res = sum;
na = t[i];
nb = t[j];
}
}
}
return d;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
res = inf;
for(int i = 0;i < n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
f[i].x = p[i].x;f[i].y = p[i].y;
}
sort(p,p+n,cmpxy);
ll ans = (ll)(n-2)*solve(0,n-1);
int cnt = 0;nc = nb;
for(int i = 0;i < n;i++){
if(na.x == f[i].x && na.y == f[i].y){
na.x = 1e9;na.y = 1e9; //改掉两个值,避免后面有重复点
continue;
}
p[cnt].x = f[i].x;p[cnt++].y = f[i].y;
}
sort(p,p+cnt,cmpxy);
ans += solve(0,cnt-1);
cnt = 0;
for(int i = 0;i < n;i++){
if(nc.x == f[i].x && nc.y == f[i].y){
nc.x = 1e9;nc.y = 1e9;
continue;
}
p[cnt].x = f[i].x;p[cnt++].y = f[i].y;
}
sort(p,p+cnt,cmpxy);
ans += solve(0,cnt-1);
printf("%lld\n",ans);
}
return 0;
}
愿你走出半生,归来仍是少年~