题目链接:

Palace

Time Limit: 8000/4000 MS (Java/Others)  

  Memory Limit: 262144/262144 K (Java/Others)

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≤105), which denotes the number of temples in this testcase.

The following n lines contains n pairs of integers, the i-th pair (x,y) (−105≤x,y≤105) 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个点,每次删除一个点,问剩下的最近点对距离和是多少;
 
思路:
 
先找一遍最近点对的两个点,删除其他n-2个点对最近点对没有影响,然后再分别删除最近点对的这两个点,再最近点对的距离,最后把这些加起来就好了;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=998244353;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-6; struct node
{
LL x,y;
}po[N],temp[N],po1[N];
int cmp1(node a,node b)
{
return a.x<b.x;
}
int cmp2(node a,node b)
{
return a.y<b.y;
}
node fa,fb;
LL ans;
LL get_dis(node a, node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
LL merge(int l,int r)
{
if(r-l<=1)
{
if(l==r)return inf;
else
{
LL dis=get_dis(po[l],po[r]);
if(dis<ans)
{
fa=po[l];
fb=po[r];
ans=dis;
}
return dis;
}
} int mid=(l+r)>>1;
LL dl=merge(l,mid),dr=merge(mid+1,r),d=min(dl,dr);
int cnt=0;
For(i,l,r)
{
if(abs(po[mid].x-po[i].x)<=d)temp[++cnt]=po[i];
}
sort(temp+1,temp+cnt+1,cmp2);
For(i,1,cnt)
{
for(int j=i+1;j<=cnt&&temp[j].y-temp[i].y<d;j++)
{
LL dis=get_dis(temp[j],temp[i]);
if(dis<d)d=dis;
if(dis<ans)
{
ans=dis;
fa=temp[j];
fb=temp[i];
}
}
}
return d;
} LL work(int l,int r)
{
ans=inf;
sort(po+l,po+r+1,cmp1);
return merge(l,r);
}
int main()
{
int t;
read(t);
while(t--)
{
int n;
read(n);
For(i,1,n)read(po1[i].x),read(po1[i].y),po[i]=po1[i]; LL sum=0;
sum+=work(1,n)*(LL)(n-2);
node faa=fa,fbb=fb;
int flag=0;
For(i,1,n)
{
if(po1[i].x==faa.x&&po1[i].y==faa.y&&flag==0)
{
flag=1;
po[i].x=1e7;
po[i].y=1e7;
continue;
}
po[i]=po1[i];
}
sum+=work(1,n);
flag=0;
For(i,1,n)
{
if(po1[i].x==fbb.x&&po1[i].y==fbb.y&&flag==0)
{
flag=1;
po[i].x=1e7;
po[i].y=1e7;
continue;
}
po[i]=po1[i];
}
sum+=work(1,n);
cout<<sum<<"\n";
} return 0;
}

  

最新文章

  1. 修改LR自带的示例程序端口号
  2. ups机制下停电提前关闭oracle数据库
  3. Linux共享库 日志方法
  4. 百度之星复赛 1004 / hdu5715 二分dp+trie
  5. JS中javascript:void(0)真正含义
  6. C#面向对象(二)
  7. Java做acm所需要的基础知识之排序问题
  8. HttpClient 请求WebApi
  9. URAL 1303
  10. 框架原理第一讲,熟悉常用的设计方式.(以MFC框架讲解)
  11. Html5+js测试题(开发版)
  12. 面向面试题和实际使用谈promise
  13. js 滚轮控制图片缩放大小和拖动
  14. 基于ROS的运动识别
  15. SqlServer数据库链接字符串
  16. Android MVP 架构一 View与Presenter
  17. Redis Cluster高可用集群在线迁移操作记录【转】
  18. Geolocation API
  19. 在JavaScript文件中用ajax方法实现省市区的三级联动
  20. Android webview背景设置为透明无效 拖动时背景闪烁黑色

热门文章

  1. python3.x对python2.x变动
  2. noip2013华容道
  3. bzoj 2694: Lcm
  4. ActiveMQ消息的延时和定时投递
  5. chrome备份网站
  6. 转:CEO, CFO, CIO, CTO, CSO是什么
  7. navicat-premium11.1.6 x64关键
  8. Arrays.asList基本用法
  9. Quartz深入浅出(二)
  10. java中InputStream String