UVa-10382 Watering Grass **

/*
* Uva-10382-Watering Grass.cpp
*
  特别注意精度。。(感觉这道题的判题有问题,开始怎么交都WA,,过两天什么都没改,再交就AC了 , 汗。。
*
* 详细代码注释:
http://apps.hi.baidu.com/share/detail/24628400
*
* Created on: 2011-10-3
* Author:
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxN = 10000 + 5;
const double eps = 1E-7;

int n, l, w;
struct SData{
double x, y;
};
SData interval[maxN];

//精度
inline int lfcmp(double x){
if (x > -eps) return 1;
else return -1;
}

//按X值排序
inline int cmp(const void *a,const void *b){
return lfcmp( (((SData*)a)->x)-(((SData*)b)->x) );
}

//区间覆盖-贪心
void greedy(){
int i = 0, ans = 0, j;
double cover = 0, tmpMax;
bool covered = false;
while(i < n){
j = i;
tmpMax = cover;
while(j<n && lfcmp(cover - (interval[j].x)) > 0){
if(lfcmp((interval[j].y) - tmpMax) > 0)
tmpMax = interval[j].y;
j++;
}
if(i == j) break;

ans++;
cover = tmpMax;
i = j;

if(lfcmp(cover - l) > 0){
covered = true;
break;
}
}
if(covered) printf("%d\n", ans);
else printf("-1\n");

}


int main(){
while(scanf("%d %d %d", &n, &l, &w) != EOF){
int p[maxN], r[maxN];
for(int i=0; i<n; i++)
scanf("%d %d", &p[i], &r[i]);

for(int i=0; i<n; i++){
//碰不到边界,不考虑
if(r[i] * 2 < w){
interval[i].x = 0;
interval[i].y = 0;
continue;
}

double d = sqrt((double)r[i]*r[i] - (double)w*w/4);
interval[i].x = p[i] - d;
interval[i].y = p[i] + d;
}

qsort(interval, n, sizeof(SData), cmp);

greedy();
}

return 0;
}

你可能感兴趣的:(water)