题解
2023-03-04 17:57:53
发布于:上海
#include<iostream>
#include<algorithm>//sort必须调用的库
using namespace std;
int x1,y1,x2,y2,n;//输入的五个数,为了cmp函数方便使用,开成全局变量
int d[100007];//预处理出来d数组,具体用途请看思路
struct node
{
int x;//记录导弹的位置信息
int y;
}w[100007];
bool cmp(node a,node b)
{
int q=(x1-a.x)(x1-a.x)+(y1-a.y)(y1-a.y);
int p=(x1-b.x)(x1-b.x)+(y1-b.y)(y1-b.y);
return(q<p);
}
int main(){
int ans=0;
cin>>x1>>y1>>x2>>y2;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>w[i].x>>w[i].y;
}
sort(w,w+n,cmp);
for(int i=n-1;i>=0;i--)
{
int h=(x2-w[i].x)(x2-w[i].x)+(y2-w[i].y)(y2-w[i].y);//预处理过程
d[i]=max(d[i+1],h);
}
ans=d[0];
for(int i=0;i<n;i++)
{
int cnt=0;
cnt=(x1-w[i].x)(x1-w[i].x)+(y1-w[i].y)(y1-w[i].y);
cnt+=d[i+1];//由于d[i]所保留的最大值包括i,而此时i属于前缀
ans=min(ans,cnt);
}
ans=min(ans,d[0]);
cout<<ans;
return 0;
}
全部评论 5
//正确答案
#include <iostream>
#include <algorithm> // The library that sort must call
using namespace std;
int x1,y1,x2,y2,n; // The five numbers entered are generated into global variables for the convenience of the cmp function
int d[100007]; // d array is preprocessed. See the idea for specific use
struct node
{
int x; // Record the position information of the missile
int y;
}w[100007];
bool cmp(node a,node b)
{
int q = (x1 - a.x) * (x1 - a.x) + (y1 - a.y) * (y1 - a.y);
int p = (x1 - b.x) * (x1 - b.x) + (y1 - b.y) * (y1 - b.y);
return q < p;
}
int main(void)
{
int ans = 0;
cin >> x1 >> y1 >> x2 >> y2;
cin >> n;
for (int i = 0;i < n;i++)
{
cin >> w[i].x >> w[i].y;
}
sort(w,w + n,cmp);
for (int i = n - 1;i >= 0;i--)
{
int h = (x2 - w[i].x) * (x2 - w[i].x) + (y2 - w[i].y) * (y2 - w[i].y); // Pretreatment process
d[i] = max(d[i + 1],h);
}
ans = d[0];
for (int i = 0;i < n;i++)
{
int cnt = 0;
cnt = (x1 - w[i].x) * (x1 - w[i].x) + (y1 - w[i].y) * (y1 - w[i].y);
cnt += d[i + 1]; // Since the maximum value reserved by d [i] includes i, i belongs to the prefix
ans = min(ans,cnt);
}
ans = min(ans,d[0]);
cout << ans;
return 0;
}2025-08-22 来自 四川
0好算,兄弟,好算
2025-08-13 来自 广东
0/code/judger/run/20250525/5043993288349057122/main.cpp: In function 'bool cmp(node, node)':
/code/judger/run/20250525/5043993288349057122/main.cpp:13:22: error: expression cannot be used as a function
13 | int q=(x1-a.x)(x1-a.x)+(y1-a.y)(y1-a.y);
| ^
/code/judger/run/20250525/5043993288349057122/main.cpp:13:39: error: expression cannot be used as a function
13 | int q=(x1-a.x)(x1-a.x)+(y1-a.y)(y1-a.y);
| ^
/code/judger/run/20250525/5043993288349057122/main.cpp:14:22: error: expression cannot be used as a function
14 | int p=(x1-b.x)(x1-b.x)+(y1-b.y)(y1-b.y);
| ^
compilation terminated due to -fmax-errors=3.2025-05-25 来自 北京
0编译错误: /code/judger/test/1836735141092229120/main.cpp: In function 'bool cmp(node, node)':
/code/judger/test/1836735141092229120/main.cpp:13:22: error: expression cannot be used as a function
int q=(x1-a.x)(x1-a.x)+(y1-a.y)(y1-a.y);
^
/code/judger/test/1836735141092229120/main.cpp:13:39: error: expression cannot be used as a function
int q=(x1-a.x)(x1-a.x)+(y1-a.y)(y1-a.y);
^
/code/judger/test/1836735141092229120/main.cpp:14:22: error: expression cannot be used as a function
int p=(x1-b.x)(x1-b.x)+(y1-b.y)(y1-b.y);
^
compilation terminated due to -fmax-errors=3.2024-09-19 来自 天津
0/code/judger/run/4966585417741631586/main11.cpp: In function 'bool cmp(node, node)':
/code/judger/run/4966585417741631586/main11.cpp:13:22: error: expression cannot be used as a function
int q=(x1-a.2024-03-24 来自 浙江
0
有帮助,赞一个