Please comment on each line if possible on the code below for me to have a better understanding of what each line is doing. It is in C++. Please also give the space and time complexity and the reason why. O(N), O(n log n), O(N^2)...etc
Please comment on each line if possible on the code below for me to have a better understanding of what each line is doing. It is in C++. Please also give the space and time complexity and the reason why. O(N), O(n log n), O(N^2)...etc. There's a picture attached of the question description.
#include <iostream>
#include <algorithm>
#include <
#include <cmath>
using namespace std;
#define F first
#define S second
bool cmp(pair<double, double> a, pair<double, double> b) {
if (a.first == b.first)
return a.second > b.second;
else
return a.first < b.first;
}
int main() {
int n; // number of sprinklers
double p, r; // position, radius
double l, w, dx; //length, width of the grass
while (cin >> n >> l >> w) {
w /= 2.0;
vector<pair<double, double> > v;
for (int i=0; i<n; i++) { //This takes O(N)
cin >> p >> r;
if (r > w){ //Exclude useless sprinklers
dx = sqrt(r*r - w*w);
v.push_back({p-dx, p+dx}); //Convert to interval coverage problem
}
}
sort(v.begin(), v.end(), cmp);
int ans = 0;
double right = 0.0;
for (int i = 0; i < v.size(); i++){ //this takes O(m^2) where m represents the size of vector
if (v[i].F > right) break;
for (int j = i+1; j < v.size() && v[j].F <= right; j++){
if (v[j].S > v[i].S){
i = j;
}
}
ans++;
right = v[i].S;
if (right >= l) break;
}
if (right >= l) cout << ans << "\n";
else cout << "-1\n";
}
return 0;
}
![Watering Grass
Description
n sprinklers are installed in a horizontal strip of grass 1 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 and 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?
coco
|+++++
|||||||||||||
0
5
10
15
20
Input
Input consists of a number of cases. The first line for each case contains integer number n, 1 and w with n ≤ 10000. The next 12 lines contains two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)
Output
For each test case output the minimum number sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output '-1'.
Sample Input 1
Sample Output 1
8 20 2
53
41
-1
12
72
10 2
13 3
16 2
19 4
3 10 1
35
93
61
3 10 1
11
91
Hint
UVa 10382](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb42a9107-65e6-4614-a858-595dd78b3016%2F1bbbbfb0-4bf2-4844-9b7d-19c3f57ecedb%2Fye6sdas_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 5 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)