Count Pairs
Description
You is given n circles centered on Y-aixs. The ith circle ' s Center is at point (I, 0) and its radius is a[i]. Count the number of pairs of circles that has at least one common point?
Input
The input should is a list of n positive integers a, each of the them should is less than or equal to 1,000,000,000 and N Shou LD is less than or equal to 100,000.
Output
The output should is the number of pairs of circles that has at least one common point in the format of the Integer.
Sample Input
1 2 3
Sample Output
3
Library queuing problems. Similar to the question number of airplanes in the sky. Place the leftmost point of each circle on the x axis with the rightmost point in the vector, mark the left or right point, note that if multiple points are in the same coordinate, the points at the left edge are to be placed in front, and then a single traversal, the left boundary is counted at the current point in the range of several circles, res + = cnt, Then the CNT is updated, i.e. ++cnt, and the right boundary is--cnt.
1 classSolution {2 Public:3 Long LongCountpairs (Constvector<int> &A) {4 Long Longres =0, cnt =0;5vector<pair<int,int>>v;6 for(inti =0; I < a.size (); ++i) {7V.push_back ({i-a[i],-1});8V.push_back ({i + a[i],1});9 }Ten sort (V.begin (), V.end ()); One for(Auto vv:v) { A if(Vv.second = =-1) { -Res + =CNT; -++CNT; the}Else { ---CNT; - } - } + returnRes; - } +};
[Meetcoder] Count Pairs