Radar Installation(利用数据有序化进行贪心选择)

English appre:

an infinite straight line:一条无限长的直线

on the coasting:在海岸线上

Cartesian coordinate system,

题目地址:http://poj.org/problem?id=1328

我的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

struct dao
{
	double x,y;
}da[10000];
bool cmp(dao a,dao b)
{
	return (a.y<b.y)||(a.y==b.y && a.x>b.x);
}

int main()
{
	int n,d;
	double x1,y1;
	int k=0;
	while(~scanf("%d%d",&n,&d)&&(n||d))
	{
		int ans=1;
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&x1,&y1);
			da[i].x=x1-sqrt((double)d*(double)d-(double)y1*y1);
			da[i].y=x1+sqrt((double)d*(double)d-(double)y1*y1); 
    		if((d-y1)<0||d<=0) ans=-1; 
    	}
	 
		if(ans==-1)
 		{
	 		printf("Case %d: -1\n",++k);
 	 		continue;
		}
  		else
    	{
    		sort(da,da+n,cmp);  
    		double t1=da[0].y;  
    		for(int i=1;i<n;i++)  
        		if(da[i].x>t1)//要是用=号,有些数据不可通过。  
        		{  	
					ans++;  
        			t1=da[i].y;//更新右范围  
        		}  
         	printf("Case %d: %d\n",++k, ans);  
    	}  
	}
}

  

转载于:https://www.cnblogs.com/dragondragon/p/11372675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值