Want to get away from the circle (sort + binary search)

Source: Internet
Author: User

Title: http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=427658

First, the circle on the x-axis to the left starting point in ascending order, and then two points to find the first starting point than the end of the circle to find a large circle (that is, away)

Importjava.util.Arrays;ImportJava.util.Comparator;ImportJava.util.Scanner;classCircle {intX,r,sta,end; Circle (intXintr) { This. x=x;  This. r=R;  This. sta=x-R;  This. end=x+R; }    } Public classN1278 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner cin=NewScanner (system.in); intn=Cin.nextint (); Circle C[]=NewCircle[n];  for(inti=0;i<n;i++) C[i]=NewCircle (Cin.nextint (), Cin.nextint ()); Arrays.sort (c,NewComparator<circle>()        {             Public intCompare (Circle c1,circle C2) {if(c1.sta>=C2.sta)return1; Else                    return-1;        }        }); intSum=0;  for(inti=0;i<n-1;i++)        {            intp=search (i,c,n); if(P!=-1) Sum+=n-p;    } System.out.print (sum); }     Public Static intSearchintPos,circle c[],intLen) {        intKey=c[pos].end,begin=pos+1,last=len-1;  while(begin<Last ) {            intMid= (begin+last)/2; if(c[mid].sta>key) last=mid; Elsebegin=mid+1; }        if(c[begin].sta>key)returnbegin; Else            return-1; }}

Want to get away from the circle (sort + binary search)

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.