有没有大佬看看我的代码哪错了?
原题链接:18.导弹拦截2024-10-02 17:28:05
发布于:浙江
#有没有大佬看看我的代码哪错了?
x1,y1,x2,y2=map(int,input().split())
n=int(input())
l=[]
lo=0
ln=0
for a in range(n):
l.append(list(map(int,input().split())))
aa=0
aaa=0
for b in range(len(l)):
i=l[b]
if lo<((x1-i[0])**2+(y1-i[1])**2):
aaa=lo
lo=((x1-i[0])**2+(y1-i[1])**2)
if ln<((x2-i[0])**2+(y2-i[1])**2):
aa=ln
ln=((x2-i[0])**2+(y2-i[1])**2)
if lo>=ln:
lo=aaa
else:
ln=aa
print(ln+lo)
全部评论 1
#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-02-15 来自 广东
0
有帮助,赞一个