您的位置:首页 > 其它

UVA - 10382 Watering Grass 贪心

2017-08-08 09:09 381 查看
题意明了

覆盖面的话 这里精度要求不是很细,可以求出每个圆能覆盖的左右边界

然后贪心选取,,规则就是左边界能覆盖的前提下 记录最大右边界,

OK

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int maxn = 10000 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
int n;
double l_, w_, max_;

struct node {
double p, R;
double l, r;
}a[maxn];
bool cmp(node a, node b) {
return a.l <= b.l;
}

void init() {
max_ = 0;
for(int i = 0; i < n; ++i) {
scanf("%lf%lf", &a[i].p, &a[i].R);
if(a[i].R <= w_/2) {
a[i].l = 0;
a[i].r = 0;
continue;
}
double d = sqrt(a[i].R * a[i].R - (w_*w_/4));
a[i].l = a[i].p - d;
a[i].r = a[i].p + d;
max_ = max(max_, a[i].r);
}
sort(a, a+n, cmp);
}

void solve() {
if(a[0].l > 0 || max_ < l_) {
cout << -1 << endl; return;
}
int ans = 0; double s = 0, r_ = 0;
for(int i = 0; i < n;) {
int j = i;
while(a[j].l <= s) {
r_ = max(r_, a[j].r);
j++;
}
if(i == j) {
cout << -1 << endl; return;
}
ans++;
s = r_; //r_ = 0;
if(s >= l_) break;
i = j;
}
cout << ans << endl;
}

int main() {

while(scanf("%d%lf%lf", &n, &l_, &w_) != EOF && n) {
init();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: