51nod 1278 The Circle (sort + Modify Step)

Source: Internet
Author: User

Topic Meaning:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278

there are n circles on the plane, and their center is on the x-axis, giving the center and radius of all the circles, and asking how many pairs of circles are absent. For example: 4 circles are located at 1, 2, 3, 4 positions, with radii of 1, 1, 2, 1, then {1, 2}, {1, 3} {2, 3} {2, 4} {3, 4} 5 pairs have intersections, only {1, 4} are separated. Input
Line 1th: A number N, indicating the number of circles (1 <= N <= 50000) 2-n + 1 lines: 2 numbers per line p, r Middle with a space, p for the position of the center, R for the radius of the circle (1 <= P, R <= 10^9)
Output
The output has a total number of circles to be absent.
Input Example
41 12 13) 24 1
Output Example
1

Topic Analysis:

/**
* Method One
* Divide the circle into left and right short points, represented by line segments, and now deform for line intersection problems
* Left endpoint Left=c-r, right endpoint right=c+r, sorted by left endpoint ascending, equal
Sort by the right endpoint in ascending order, or scan, for each line I, when a later bar
* The left end of line J is greater than the right end of I, then all subsequent segments are
* Time complexity of O (Nlog (n) +n^2)
* The data for this question is 50000, the normal O (n*n) time will definitely be timed out
* Now we can consider the method of changing the Step size optimization algorithm (can only reduce the coefficient, but sometimes really good),
* I optimize in the query, the other step is 100, if the current J satisfies the condition, then first satisfies
* The condition of J must be within 100 of the J before, only the first 100 of the query J can be,
* Time Complexity O (N*log (n) +n* (n/100+100)); Reduce your n^2 factor by 100 times times
*************************************************************************
* Method Two thank Dava
* Place the start and end points of all circles in an array, sorted by size (the left and right short points of the circle)
* If the starting point of a circle is equal to the end of another circle, the starting point is in front
* Define Structure body
*struct{
* A long long d;//segment node
* int tmp;//marks the beginning (0) and the end point (1),
*}
* Record all start and end points directly and quickly,
* Make the number of num= round, meet the starting point num--, meet the end Sum+=num
*/

AC Code, Method 1

#include <iostream> #include <string> #include <algorithm>using namespace std;struct node{int left, right;};    Node P[50005];int CMP (node A,node b) {if (a.left<b.left) return 1;    else if (a.left==b.left&&a.right<b.right) return 1; return 0;}    int main () {int c,r,n;            while (Cin>>n) {for (int i=0;i<n;i++) {cin>>c>>r;            P[i].left=c-r;        P[i].right=c+r;        } sort (p,p+n,cmp);        int i,j,sum=0,k;            for (i=0;i<n;i++) {for (j=i+100;j<n;j+=100) {////step = (p[j].left>p[i].right) break;            } if (j>n) j=n; for (k=j-1;k>0&&k>j-101;k--) {//Scan 100 elements between if (p[k].left<=p[i].right) {sum +=n-(k+1);                Break    }}} cout<<sum<<endl; } return 0;}

AC code, method two

#include <iostream> #include <algorithm>using namespace std;struct node{    long long d;//line node    int flag;//Mark Start (0) and end point (1),}p[100005];int CMP (node A,node b) {    if (A.D<B.D) return 1;    else if (a.d==b.d&&a.flag==0) return 1;    return 0;} int main () {    int c,r,n;    while (cin>>n) {        int k=0;        for (int i=0;i<n;i++) {            cin>>c>>r;            P[k].d=c-r;            p[k++].flag=0;//beginning            p[k].d=c+r;            p[k++].flag=1;//End        }        //cout<<k<<endl;        Sort (p,p+k,cmp);        int sum=0,num=n;        for (int i=0;i<k;i++) {            //cout<<p[i].d<< "" <<p[i].flag<<endl;            if (p[i].flag==0) num--;            else if (p[i].flag==1) sum+=num;        }        cout<<sum<<endl;    }    return 0;}



51nod 1278 The Circle (sort + Modify Step)

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.