5
22
2015
0

poj 1328 Radar Installation

//总结:细节决定成败。
//判断是否输出-1的部分break,continue细节弄错错了好久
//大致思路:对于每个岛,x正负sqrt(d^2-y^2)的线段上的点都可以包含
//所以就是对于n条线段进行区间取点问题。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#define sqr(x) (x)*(x)
using namespace std;

struct Point{
	double x;
	double y;
}p[1000];

bool cmp(Point a,Point b){
	if (a.y!=b.y) return a.y<b.y;
	return a.x>b.x;
}

int main()
{
	int c=0,n,d;
	while (cin>>n>>d){
		if (n==0 && d==0) break;
    bool flag=false;
		for (int i=0;i<n;++i){
			cin>>p[i].x>>p[i].y;
			if (!flag && p[i].y>d){
				cout<<"Case "<<(++c)<<": -1\n";
        flag=true;
			}
			double t=sqrt(sqr(d)-sqr(p[i].y));
			p[i].y=p[i].x;
			p[i].x-=t; p[i].y+=t;
		}
    if (flag) continue;

		sort(p,p+n,cmp);
		int ans=1;
		double cur=p[0].y;
		for (int i=1;i<n;++i)
			if (cur<p[i].x){
				cur=p[i].y; ++ans;
			}
		cout<<"Case "<<(++c)<<": "<<ans<<endl;
	}
	return 0;
}
Category: 题解 | Tags: 贪心 POJ

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com