Ultraviolet A 10382-Watering Grass

Source: Internet
Author: User

Original question:
N sprinklers are installed in a horizontal strip of grass l meters long and w meters 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 instances. the first line for each case contains integer numbers n, l and w with n <= 10000. the next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture abve into 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 impossible to water the entire strip output-1.
Sample input
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1

Sample output
6
2
-1


Question:
A lawn with a length of l and w can be installed in n locations on its horizontal center, the coverage range of the sprinkler in each position is the circle with their own radius ri. Find the minimum number of sprinkler devices required.

Analysis and Summary:
The key to this question is conversion.

The figure shows that the effective coverage of a sprinkler is the rectangle in the middle of the circle. Therefore, during the input, preprocessing is performed to convert the coordinates on the left and on the right of the rectangle. In this way, it turns into a classic range coverage problem.


Code:
[Cpp]
/*
* Ultraviolet A: 10382-Watering Grass
* Greedy: minimum coverage
* Result: Accept
* Time: 0.016 s
* Author: D_Double
*/
# Include <iostream>
# Include <cmath>
# Include <cstdio>
# Include <algorithm>
# Define maxn10005
Using namespace std;
Int n, nIndex;
Double l, w;
 
 
Struct Node {
Double left;
Double right;
Friend bool operator <(const Node & a, const Node & B ){
Return a. left <B. left;
}
} Arr [MAXN];
 
Int main (){
Double p, r;
While (scanf ("% d % lf", & n, & l, & w )! = EOF ){
NIndex = 0;
For (int I = 0; I <n; ++ I ){
Scanf ("% lf", & p, & r );
If (w/2> = r)
Continue; // do not consider if the diameter is smaller than the width
Double t = sqrt (r * r-w * w/4.0 );
Arr [nIndex]. left = p-t;
Arr [nIndex]. right = p + t;
++ NIndex;
}
 
Sort (arr, arr + nIndex );
Int cnt = 0;
Double left = 0, right = 0;
Bool flag = false;
 
If (arr [0]. left <= 0 ){
Int I = 0;
 
While (I <nIndex ){
 
Int j = I;
While (j <nIndex & left> = arr [j]. left ){
If (arr [j]. right> right)
Right = arr [j]. right;
++ J;
}
If (j = I) break; // if the above loop body is not executed, it indicates that it is not satisfied in the future.
 
++ Cnt;
Left = right;
I = j;
 
If (left> = l ){
Flag = true;
Break;
}
}
}
If (flag) printf ("% d \ n", cnt );
Else printf ("-1 \ n ");
}
Return 0;
}

Author: shuangde800

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.