zoukankan      html  css  js  c++  java
  • codevs1128 导弹拦截

    题目描述 Description

    经过11 年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截。当工作半径为0 时,则能够拦截与它位置恰好相同的导弹。但该导弹拦截系统也存在这样的缺陷:每套系统每天只能设定一次工作半径。而当天的使用代价,就是所有系统工作半径的平方和。
    某天,雷达捕捉到敌国的导弹来袭。由于该系统尚处于试验阶段,所以只有两套系统投入工作。如果现在的要求是拦截所有的导弹,请计算这一天的最小使用代价。

    数据范围
    对于10%的数据,N = 1
    对于20%的数据,1 ≤ N ≤ 2
    对于40%的数据,1 ≤ N ≤ 100
    对于70%的数据,1 ≤ N ≤ 1000
    对于100%的数据,1 ≤ N ≤ 100000,且所有坐标分量的绝对值都不超过1000。

    输入描述 Input Description

    第一行包含4 个整数x1、y1、x2、y2,每两个整数之间用一个空格隔开,表示这两套导弹拦截系统的坐标分别为(x1, y1)、(x2, y2)。
    第二行包含1 个整数N,表示有N 颗导弹。接下来N 行,每行两个整数x、y,中间用一个空格隔开,表示一颗导弹的坐标(x, y)。不同导弹的坐标可能相同。

    输出描述 Output Description

    输出只有一行,包含一个整数,即当天的最小使用代价。

    样例输入 Sample Input

    0 0 10 0
    2
    -3 3
    10 0

    样例输出 Sample Output

    18

    数据范围及提示 Data Size & Hint

    两个点(x1, y1)、(x2, y2)之间距离的平方是(x1− x2)^2+(y1−y2)^2。
    两套系统工作半径r1、r2 的平方和,是指r1、r2 分别取平方后再求和,即r1^2+r2^2。

    思路:

    贪心,把所有导弹到两个拦截装置的距离求出来,然后按照与第一个装置的距离为关键字进行排序,然后从大到小算出两个装置的设置半径,最后输出结果

    代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    const int maxn = 10000000;
    int n,xa,xb,ya,yb,total = maxn;
    int x[maxn],y[maxn];
    
    struct bomb{
        int s1,s2,num;
    };
    bomb a[maxn];
    
    bool cmp(bomb n1,bomb n2){
        if(n1.s1 < n2.s1) return true;
        else return false;
    }
    int main(){    
        cin>>xa>>ya>>xb>>yb>>n;
    
        
        int r1 = 0,r2 = 0,temp = 0,total = maxn;
        for(r1 = 0;r1 < n;r1++){
           cin>>x[r1]>>y[r1];
           a[r1].s1 = (x[r1] - xa) * (x[r1] - xa) + (y[r1] - ya) * (y[r1] - ya);
           a[r1].s2 = (x[r1] - xb) * (x[r1] - xb) + (y[r1] - yb) * (y[r1] - yb);
           a[r1].num = r1;
           
    }
    
        sort(a,a+n,cmp);
        r1 = n -1;
        r2 = 0;
    
        for(r1 = n - 1;r1 >= 0;r1--){
            r2 = r1 + 1;
            if(a[r2].s2 > temp && r2 < n) temp = a[r2].s2;
                
    
            if(total > temp + a[r1].s1) total = temp + a[r1].s1;
        }
            if(a[0].s2 > temp) temp = a[0].s2;    
            if(total > temp) total = temp;
    
        
        cout<<total;
        return 0;
    }
  • 相关阅读:
    python+selenium框架
    django--form组件
    python +selenium上传文件
    python--UI---登录---验证码
    python+selenium button定位方法
    css-定位技术
    css-盒子模型
    css-元素分类
    序列化
    FileUploadController
  • 原文地址:https://www.cnblogs.com/hyfer/p/5812493.html
Copyright © 2011-2022 走看看