Va 10382-Watering Grass (greedy algorithm, range coverage problem)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.