-
Notifications
You must be signed in to change notification settings - Fork 9
/
10382-Watering Grass.cpp
35 lines (34 loc) · 1.06 KB
/
10382-Watering Grass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,l,w,p,r;
while(scanf("%d %d %d",&n,&l,&w) != EOF){
vector<pair<double,double>> intervals;
for(int i=0;i<n;i++){
scanf("%d %d",&p,&r);
double dx = sqrt((double)r*r-(w/2.0)*(w/2.0));
if(dx == dx){ // check for nan
intervals.push_back({(double)p-dx,(double)p+dx});
}
}
sort(intervals.begin(),intervals.end());
double cur = 0;
int res = 0;
for(int i=0;i<intervals.size() && cur < l;i++){
// not intersecting, impossible
if(intervals[i].first > cur) {
break;
}
res++;
// find out furthest interval to right we can reach
double furthest = intervals[i].second;
while(i+1<intervals.size() && intervals[i+1].first <= cur){
furthest = max(intervals[++i].second,furthest);
}
cur = furthest;
}
if(cur < l) res = -1;
printf("%d\n",res);
}
}