[Meetcoder] Count Pairs

Source: Internet
Author: User

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

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.