UVALIVE 2519 Radar Installation 區間選點問題

#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.1415926535897932626
using namespace std;
#define MAXN 1005
int 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章