zoukankan      html  css  js  c++  java
  • 最小安装雷达数量

    题目:海岸线一条无线长的直线,一边是陆地,一边是海洋,在海洋中有小岛,需要雷达监测,需要最少雷达数。

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    public class Radar_Installation {
        static class Radar{
            double l;
            double r;
            public Radar(double l, double r){
                this.l = l;
                this.r = r;
            }
        }
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();//小岛数量
            int d = sc.nextInt();//雷达监测半径
            int x , y;
            double l, r;
            int count = 1;
            ArrayList<Radar> list = new ArrayList<Radar>();
            for(int i=0; i<n; i++){
                x = sc.nextInt();
                y = sc.nextInt();
                l = x - Math.sqrt((d*d - y * y));//计算雷达最小x
                r = x + Math.sqrt((d*d - y * y));//计算雷达最大x
                list.add(new Radar(l, r));
            }
            Collections.sort(list, new Sort());//按最有边界进行排序
            Radar radar = list.get(0);
            for(int i=1; i<list.size(); i++    ){
                if(list.get(i).l > radar.r){//当第下一个雷达的左边界大于上一个雷达的右边界则,需再增加一个雷达
                    count++;
                    radar = list.get(i);
                }else if(list.get(i).r < radar.r){//当下一个雷达的右边界小于当前雷达的右边界需要更新。
                    radar = list.get(i);
                }
            }
            System.out.println(count);
            sc.close();
        }
        //对arraylist进行排序
        static class Sort implements Comparator<Radar>{
    
            @Override
            public int compare(Radar o1, Radar o2) {
                
                return o1.r == o2.r ? 0 : o1.r > o2.r ? 1 : -1;
            }
            
        }
    }
  • 相关阅读:
    UVA 11354
    HDU 4081 Qin Shi Huang's National Road System 最小/次小生成树的性质
    UVA 10269 Adventure of Super Mario floyd dp
    UVA 11280 Flying to Fredericton 最短路DP
    【专题】树状数组
    【专题】Subsequence
    共享python代码模块
    完全背包
    POJ 3253 Fence Repair
    POJ 3069 Saruman's Army
  • 原文地址:https://www.cnblogs.com/woniu4/p/4629246.html
Copyright © 2011-2022 走看看