前置芝士
平面两点之间距离公式:
解题思路
先预处理每个点到两个拦截系统距离。
注意:距离可以不用开方直接相加求出代价,因为后面要求使用代价,就是所有系统工作半径的平方和,两两相互抵消了。
再按到第一个系统的距离从小到大排序,这样只需要将 个点到第二个系统的距离取最大值即可。
代码
#include<bits/stdc++.h>
using namespace std;
long long sx,sy,tx,ty,n;
long long x,y,ans=1e12,cnt;
struct node{
long long l,r;//距离2个系统的距离
}a[100100];
bool cmp(node p,node q){
return p.l<q.l;
}
int main(){
cin>>sx>>sy>>tx>>ty>>n;
for (int i=1;i<=n;i++){
cin>>x>>y;
a[i].l=(x-sx)*(x-sx)+(y-sy)*(y-sy);
a[i].r=(x-tx)*(x-tx)+(y-ty)*(y-ty);//计算距离(省略sqrt)
}
sort(a+1,a+n+1,cmp);//按第一个系统距离从小到大排序
ans=a[n].l;//距离第一个系统最远的导弹
for (int i=n;i>=1;i--){
ans=min(ans,a[i].l+cnt);
cnt=max(cnt,a[i].r);//到第二个系统的距离取最大值
}
cout<<min(ans,cnt);//求最小代价
return 0;
}