UVALIVE 2519 Radar Installation 区间选点问题

来源:互联网 发布:京东mac客户端下载 编辑:程序博客网 时间:2024/04/30 04:07
#include <map>#include <set>#include <list>#include <cmath>#include<cctype>#include <ctime>#include <deque>#include <stack>#include <queue>#include <cstdio>#include <string>#include <vector>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define PI 3.1415926535897932626using namespace std;#define MAXN 1005int gcd(int a, int b){return a % b == 0 ? b : gcd(b, a % b);}int N,R;bool vis[MAXN];struct node{    double l,r;    friend bool operator <(const node &a, const node &b)    {        if (a.r==b.r)return a.l<b.l;        return a.r<b.r;    }}src[MAXN];int main(){    //freopen("sample.txt","r",stdin);    int kase=1;    while (scanf("%d%d",&N,&R)!=EOF)    {        if (N==0 && R==0) break;        bool found=false;        int x,y;        for (int i=0;i<N;i++)        {            scanf("%d%d",&x,&y);            if (y>R) found=true;            double t=sqrt((double)(R*R-y*y));            src[i].l=x-t;            src[i].r=x+t;        }        printf("Case %d: ",kase++);        if (found) printf("-1\n");        else        {            sort(src,src+N);            memset(vis,false,sizeof(vis));            int ans=0;            double tmp;            for (int i=0;i<N;i++)            {                if (vis[i]) continue;                ans++;                int j=i+1;                tmp=src[i].r;                while (j<N){                    if (!vis[j] && src[j].l<=tmp) vis[j]=true; j++;                }            }            printf("%d\n",ans);        }    }    return 0;}

0 0
原创粉丝点击