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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 <vector>

#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
Transcribed Image Text: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
Expert Solution
steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY