1637: [Usaco2007 mar]balanced Lineup

Source: Internet
Author: User

1637: [Usaco2007 mar]balanced lineup time limit:5 Sec Memory limit:64 MB
submit:393 solved:263
[Submit] [Status] Description

Farmer John decided to take a picture of his cows, and he let N (1≤n≤50,000) cows stand in a straight line, each with its coordinates (range: 0..1,000,000,000) and Race (0 or 1). Farmer John has always been fond of doing something extraordinary, of course, the photograph is no exception. He only photographed some of the cows, and the team had to be "balanced". The balanced lineup refers to the equal number of cattle in a group of cattle, race 0 and Race 1. Please figure out the widest range to balance the herd within this range. The size of the interval is the coordinates of the right-most cow in the interval minus the coordinates of the most-done cow. In the input, each race has at least one cow and no two cows have the same coordinates.

Input

Line 1: An integer: N rows 2..N + 1: Two integers per line, race ID and x-coordinate.

Output

Line 1: An integer, lineup balances the size of the largest interval.

Sample Input7
0 11
1 10
1 25
1 12
1 4
0 13
1 22
Sample Output11
Enter a description

There are 7 cows, like this on the axis.


1 1 0 1 0 1 1
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output description

Cattle #1 (at one), #4 (at a), #6 (at), #7 (at 22) make up a maximum interval of equilibrium, size 22-11 = 11 unit length.

<--------balanced-------->
1 1 0 1 0 1 1
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
HINT Source

Silver

The solution: this, although a one-time AC, but there are a lot of twists and turns-a long time to think about the idea, and then write and write, and then halfway through to be interrupted to do something else, and then come back to forget, and then re-blind toss the code more and more chaotic heart more and more annoying, and then finally seems to have Then accept, then did not then (think parade was interrupted thinking is just killing ah have wood there) ... OK, say the idea-first according to the position of each cow order, then if the race is 0 -1,1 1, then the problem is converted to seek the longest and 0 of the interval, the next is good to do-record the same prefix and the earliest occurrence of the time and the latest occurrence, and then find the maximum time difference can be ...

varI,j,k,l,m,n:longint; A,b,c,f:Array[0..100000] ofLongint; D,e:Array[-60000..60000] ofLongint;procedureSwapvarx,y:longint); varZ:longint; beginZ:=x;x:=y;y:=Z; End;proceduresort (l,r:longint); varI,j,x,y:longint; beginI:=l;j:=R; X:=a[(L+r)Div 2]; Repeat                      whileA[i]<x DoInc (I);  whileA[j]>x DoDec (j); ifI<=j Then                        beginswap (a[i],a[j]);                             Swap (b[i],b[j]);                        Inc (I);d EC (j); End; untilI>J; ifL<j Thensort (l,j); ifI<r Thensort (i,r); End;beginREADLN (n);  fori:=1  toN Do         beginreadln (B[i],a[i]); ifb[i]=0  Thenb[i]:=-1; End; Sort (1, N); c[0]:=0;  fori:=1  toN DoC[i]:=c[i-1]+B[i]; Fillchar (d,sizeof (d),-1); Fillchar (E,sizeof (e),-1);  fori:=0  toN Do         begin              if(d[c[i]]=-1) Thend[c[i]]:=i; E[c[i]]:=i; End; L:=0;  fori:=-50000  to 50000  Do         begin              ifE[i]<=d[i] Thencontinue; if(a[e[i]]-a[d[i]+1]) >l Thenl:=a[e[i]]-a[d[i]+1]; End; Writeln (l);End. 

1637: [Usaco2007 mar]balanced Lineup

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.