zoj1360/poj1328 Radar Installation(贪心)

对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题。数轴上有n个闭区间[ai,bi],取尽量少的点,使得每个区间内都至少有一个点。选法是:把区间按右端点从小到大排序(右端点相同时按左端点从大到小),然后每个没选的区间选最右边的一个点即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<deque>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-10;
const int INF=1000000000;
const int maxn=1000+10;

int n,cas=1;
double d,x,y;
struct Line
{
    double l,r;
    bool used;
    Line(double ll=0,double rr=0,bool uu=false):l(ll),r(rr),used(uu) {}
    Line(const Line& C)
    {
        l=C.l;
        r=C.r;
        used=C.used;
    }
    bool operator < (const Line& B) const
    {
        if(r!=B.r)
            return r<B.r;
        else
            return l>B.l;
    }
} a[maxn];

int main()
{
    //freopen("in2.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%lf",&n,&d)==2)
    {
        if(n==0&&d==0) break;
        printf("Case %d: ",cas++);
        int ans=0;
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&x,&y);
            if(y>d)
            {
                ans=-1;
                break;
            }
            a[i].l=x-sqrt(d*d-y*y);
            a[i].r=x+sqrt(d*d-y*y);
            //cout<<a[i].l<<‘ ‘<<a[i].r<<endl;
            a[i].used=false;
        }
        if(ans==-1)
        {
            printf("-1\n");
        }
        else
        {
            sort(a,a+n);
            for(int i=0; i<n; i++)
            {
                //cout<<a[i].l<<‘ ‘<<a[i].r<<endl;
                if(a[i].used==false)
                {
                    a[i].used=true;
                    ans++;
                    for(int j=i+1; j<n; j++)
                    {
                        if(a[j].l<=a[i].r)
                        {
                            a[j].used=true;
                        }
                        else break;
                    }
                }
            }
            printf("%d\n",ans);
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
时间: 2024-08-05 16:35:53

zoj1360/poj1328 Radar Installation(贪心)的相关文章

ZOJ-1360 || POJ-1328——Radar Installation

ZOJ地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=360 POJ地址:http://poj.org/problem?id=1328 解题思路:贪心 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5  6 struct P{ 7     double x, y

POJ1328 Radar Installation 【贪心&amp;#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

poj1328 Radar Installation(贪心)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1328 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in th

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ1328 Radar Installation 【贪心&#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

[POJ1328] Radar Installation

Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, s