POJ 1328 Radar Installation G++ 贪心 背


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cstdio> 
using namespace std;
//经典贪心题   右点排序右点放雷达   背 
//看博友分析 
bool cmp(pair<double,double> x, pair<double, double> y)
{
	if(x.second==y.second)
	{
		return x.first<y.first;
	}else
	{
		return x.second<y.second;
	}
} 
int main()
{
	int tag=0;
	while(1)
	{
		tag++;
		vector<pair<double, double> > ve;
		double n,r;
		scanf("%lf%lf",&n,&r);
		//cin>>n>>r;
		if(n==0 && r==0)
		{
			break;
		}
		int flag=0;
		for(int i=0;i<n;i++)
		{
			double a,b;
			scanf("%lf%lf",&a,&b);
			//cin>>a>>b;
			if(b>r|| b<0 || r<0)//抄博友 
			{
				flag=1;
			}else
			{
				ve.push_back(make_pair(a-sqrt(r*r-b*b), a+sqrt(r*r-b*b)));
				//cout<<a-sqrt(r*r-b*b)<<" "<<a+sqrt(r*r-b*b)<<endl;				
			}
		}
		if(flag==1)
		{
			cout<<"Case "<<tag<<": "<<-1<<endl;
			continue;
		}
		sort(ve.begin(),ve.end(),cmp);
		double lim=ve[0].second;
		int jg=1;
		//cout<<endl;
		for(int i=0;i<ve.size();i++)
		{
			//cout<<ve[i].first<<" "<<ve[i].second<<endl;
			if(ve[i].first>lim)//不相交
			{
				lim=ve[i].second; 
				jg++;
			}
		}
		cout<<"Case "<<tag<<": "<<jg<<endl;
	} 
	return 0;
}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值