Problem E
Watering Grass
Input:Standard input
Output:Standard output
Time Limit:3 seconds
NSprinklers are installed in a horizontal strip of grassLMeters long andWMeters wide. each sprinkler is installed at the horizontal center line of the strip. for each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.
What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?
Input
Input consists of a number of cases. The first line for each case contains integer numbersN,LAndWWithN<= 10000. The nextNLines contain two integers giving the position of a sprinkler and its radius of operation. (The picture abve has strates the first case from the sample input .)
Output
For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impZ finished? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> Principal + CjxwPjMgNTwvcD4KPHA + Principal + CjxwPjkgMTwvcD4KPHA + principal =" brush: java; ">Sample Output
6
2
-1
(Regionals 2002 Warm-up Contest, Problem setter: Piotr Rudnicku)
Ideas:
Greedy thinking, transforming the problem into a range coverage problem, taking the upper border of the lawn as the range to be covered, and calculating the range covered by each sprinkler, which cannot be covered, then, the sprinkler is arranged in ascending order on the left side of the coverage area.
The rightmost vertex to be covered is the initial value of the rightmost value 0. traverse the sprinkler and find a sprinkler with the largest Right Border Covering the rightmost, then, use the right border covered by the sprinkler as the new rightmost and repeat the process until the entire lawn is covered.
# Include
# Include
# Include
Using namespace std; # define MAX_SIZE 10000 struct Sprinkler {double left; double right; bool operator <(const Sprinkler & s) const {return left <s. left ;}} sprinklers [MAX_SIZE + 5]; int WaterTheGrass (int m, int l) {double rightmost = 0.0; int count = 0; int I, j; for (I = 0; I <m; I = j) {if (sprinklers [I]. left> rightmost) break; for (j = I + 1; j <m & sprinklers [j]. left <= rightmost; ++ j) {if (sprinklers [j]. right> sprinklers [I]. right) {I = j ;}}++ count; rightmost = sprinklers [I]. right; if (rightmost> = l) break;} if (rightmost> = l) {return count;} return-1;} int main (void) {int n, l; double w; while (cin> n> l> w) {w/= 2.0; int I, m = 0; for (I = 0; I <n; ++ I) {int p, r; scanf ("% d", & p, & r); if (r> w) {double halfCoveredLen = sqrt (double) r * r-w * w); // note that it is converted to the double type, wrong several times... sprinklers [m]. left = (double) p-halfCoveredLen; sprinklers [m ++]. right = (double) p + halfCoveredLen;} sort (sprinklers, sprinklers + m); cout <WaterTheGrass (m, l) <endl;} return 0 ;}
Welcome to reprint, please respect the author, reprint please indicate the source: http://blog.csdn.net/code_pang/article/details/17919533