Alg_problem: number_of_disc_intersections

Source: Internet
Author: User

Given an array a of N integers we draw n discs in a 2D plane, such that I-th disc has center in (0, I) and a radius a [I]. we say that k-th disc and J-th disc intersect, IF and k-th and J-th discs have at least one common point.

Write a function

Int number_of_disc_intersections (INT [] );

Which given an array a describing n discs as explained abve, returns the number of pairs of intersecting discs. For example, given n = 6 and

There are 11 pairs of intersecting discs:

 
0th and 1st
0th and 2nd
0th and 4th
1st and 2nd
1st and 3rd
1st and 4th
1st and 5th
2nd and 3rd
2nd and 4th
3rd and 4th
4th and 5th

So the function shocould return 11.

The function shocould return-1 if the number of intersecting pairs exceeds 10,000,000. The function may assume that N does not exceed 10,000,000.

 

# Include <stdio. h>
# Include <vector>

Int number_of_disc_intersections (const STD: vector <int> & ){
// Write your code here
Size_t n = A. Size ();
If (n = 0) Return-1;

long num = 0;
for (size_t I = 0; I {< br> for (size_t J = I + 1; j {< br> If (J-I <= A [I] + A [J]/* & J-I + A [I]> = A [J] & J-I + A [J]> = A [I] */)
{< br> // printf ("% d \ n", I, j);
num ++;
If (Num> = 10000000)
return-1;
}< BR >}

Return num;
}

Int main ()
{
Int A [] = {1, 5, 2, 1, 4, 0 };
STD: vector <int> A (A, A + sizeof (a)/sizeof (A [0]);
Int I = number_of_disc_intersections ();
Printf ("% d \ n", I );
}

 

 

Http://stackoverflow.com/questions/4801242/algorithm-to-calculate-number-of-intersecting-discs

 

Http://www.devcomments.com/q490073/Algorithm-to-calculate-number-intersecting-discs

 

 

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.