This is almost the same as that of the ultraviolet A 10020, but here we need to convert the circular area into a long stripe area that can be covered (a small alignment theorem)
Learn other people's code and practice using the STL vector container
Here is a small trick to use a small amount of EPS to compensate for the error in floating point operations.
1 // # define local 2 # include <vector> 3 # include <cstdio> 4 # include <cmath> 5 # include <algorithm> 6 # include <functional> 7 using namespace std; 8 9 const int maxn = 10240; 10 const double EPS = 1e-11; 11 12 struct range13 {14 double A, B; 15 inline bool operator <(const range & RHs) const16 {17 return a <RHS. A | (A = RHS. A & B <RHS. b); 18} 19}; 20 21 int main (void) 22 {23 # ifdef local24 freopen (" 10382in.txt "," r ", stdin); 25 # endif26 27 int n, l, W; 28 double lenth, width; 29 vector <range> ranges; 30 ranges. reserve (maxn); // The reserve () function sets the capacity in advance to avoid inefficient 31 32 While (scanf ("% d ", & N, & L, & W) = 3) 33 {34 lenth = (double) L; 35 width = W/2.0; 36 ranges. clear (); 37 38 for (INT I = 0; I <n; ++ I) 39 {40 int position, radius; 41 double XW; 42 range; 43 44 scanf ("% d", & position, & radius); 45 If (radius * 2 <= W) continue; 46 XW = SQRT (double) radius * radius-width * width); 47 48 range. A = position-XW; 49 If (range. a> lenth + EPS) continue; 50 else if (range. a-EPS <0.0) range. A = 0.0; 51 range. B = Position + XW; 52 ranges. push_back (range); 53} 54 55 sort (ranges. begin (), ranges. end (); 56 57 int mincover = 0; 58 Double Start = 0.0, end = 0.0; 59 for (vector <range >:: iterator Pr = ranges. begin (); PR! = Ranges. end ();) 60 {61 start = end; 62 if (pr-> A> Start + EPS) 63 {64 mincover =-1; 65 break; 66} 67 + + mincover; 68 while (PR! = Ranges. end () & pr-> A <= Start) 69 {70 If (pr-> B> end + EPS) End = Pr-> B; 71 + + PR; 72} 73 If (end> lenth + EPS) 74 break; 75} 76 if (end + EPS <lenth) mincover =-1; 77 printf ("% d \ n ", mincover); 78} 79 return 0; 80}
Code Jun
Ultraviolet A 10382 (range coverage) watering grass