您的位置:首页 > 其它

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

2013-12-12 22:06 507 查看
/*
* POJ_1328.cpp
*
*  Created on: 2013年11月18日
*      Author: Administrator
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 1010;

struct tt{
double l,r;
}p[maxn];

int n,d;

bool flag;
void init(){
flag = true;

int i;
double x,y;

for(i = 1 ; i <= n ; ++i){
scanf("%lf%lf",&x,&y);

if(d < y){
flag = false;//***改成这种写法....不要使用return.因为您将读数据的操作也放在这里了,而测试数据已不满足就return的话,可能会导致数据没有读完而报RE
}

double h = sqrt(d*d - y*y);
p[i].l = x - h;
p[i].r = x + h;
}
}

bool cmp(tt a, tt b){
if( b.r - a.r > 10e-7){
return true;
}

if(abs(a.r - b.r) < 10e-7 && ( b.l - a.l > 10e-7)){
return true;
}

return false;
}

void work(){
if( d == -1){
printf("-1\n");
return ;
}

sort(p+1,p+1+n,cmp);

int ans = 0;
double last = -10000.0;
int i;
for(i = 1 ; i <= n ; ++i){
if(p[i].l <= last){
if(p[i].r <= last){//***这个一定要有,用于处理(-1,5)、(1,3)这种情况
last = p[i].r;
}
continue;
}

ans++;
last = p[i].r;
}

printf("%d\n",ans);
}

int main(){
int counter = 1;
while(scanf("%d%d",&n,&d)!=EOF,n||d){
printf("Case %d: ",counter++);
init();

if(!flag){
printf("-1\n");
}else{
work();
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐