• poj1328贪心中的区间问题


    题意:给定海岛个数、雷达半径以及各海岛坐标,求能覆盖所有海岛的最小雷达数。

    思路:先对每个海岛求一个区间:即能覆盖它的所有雷达的圆心所构成的区间。然后对区间排序,定义一个最右点over,依次延伸over,如果over不在某个区间内,那么消耗一颗雷达,over更新为该区间的最右端,否则end更新为起点在over内的所有区间的最小右端点。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <bitset>
    #include <cmath>
    #include <queue>
    #include <stack>
    using namespace std;
    const int maxn=1010;
    typedef struct P
    {
        double start,over;
    }P;
    P point[maxn];
    bool cmp(P a,P b)
    {
        return a.start<b.start;
    }
    int main()
    {
        int n;
        double r;
        int cas=0;
        while(cin>>n>>r)
        {
            if(n==0&&r==0) break;
            int res=0;
            for(int i=0;i<n;i++)
            {
                double x,y;
                cin>>x>>y;
                if(res==-1) continue;
                if(y>r){
                    res=-1;
                    continue;
                }
                double tt=sqrt(r*r-y*y);
                point[i].start=x-tt;
                point[i].over=x+tt;
            }
            if(res==-1)
            {
                cout << "Case " << ++cas<< ": " << res << endl;
                continue;
            }
            sort(point,point+n,cmp);
            double mi=-0x3ffff;
            for(int i=0;i<n;i++)
            {
                if(mi<point[i].start){
                    res++;
                    mi=point[i].over;
                }
                else if(mi>point[i].over){
                    mi=point[i].over;
                }
            }
            cout << "Case " << ++cas<< ": " << res << endl;
        }
        return 0;
    }
    我的代码
  • 相关阅读:
    UVa 727
    UVa 11495
    UVa 299
    UVa 10194
    UVa 146
    10025
    Tug of War POJ 2576 DP(类似背包)
    Problem A: Freckles UVA 10034 裸生成树
    UVA 562
    CF DIV 2 206 C. Vasya and Robot
  • 原文地址:https://www.cnblogs.com/wolf940509/p/5325987.html
Copyright © 2020-2023  润新知