跳到正文

题解

题解:P1158 [NOIP 2010 普及组] 导弹拦截

Zhc0731约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P1158

版本

  1. v1当前

前置芝士

平面两点之间距离公式: (x1−x2)2+(y1−y2)2\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}

解题思路

先预处理每个点到两个拦截系统距离。

注意:距离可以不用开方直接相加求出代价,因为后面要求使用代价,就是所有系统工作半径的平方和,两两相互抵消了。

再按到第一个系统的距离从小到大排序,这样只需要将 nn 个点到第二个系统的距离取最大值即可。

代码

C++
#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;
}
原文