1303: [CQOI2009] Median figure

Source: Internet
Author: User

1303: [CQOI2009] Median figure time limit:1 Sec Memory limit:162 MB
submit:1383 solved:902
[Submit] [Status] Description gives a permutation of the 1~n, and counts the number of consecutive sub-sequences with an odd length of time in this arrangement, which is B. The median is the number in the middle when all the elements are arranged from small to large. Input first acts two positive integers n and b, and the second behavior 1~n the arrangement. Output outputs an integer that is the number of consecutive sub-sequences with a median of B. Sample Input7 4
5 7 2 4 3 1 6Sample Output4HINT

The third example explains: {4}, {7,2,4}, {5,7,2,4,3}, and {5,7,2,4,3,1,6}
n<=100000

Source

The following: or prefix and related--will be greater than the median value is set to 1, less than set to-1, equal to 0, you can create a prefix and arrays--in the array as long as a certain two locations (note: The range is 0-n) the value is equal, the existence of a paragraph and 0 of the segment, only a sequence of statistics-- Special Note: Be sure to consider whether the length of this paragraph is odd or even, because if it is odd, it must be a legitimate subsequence, if it is an even number, it must not be a legitimate sub-sequence-that is, there is a clear and definite connection between the two, so only the length of the odd count into the total, Pay attention to (Hansbug: The online puzzle seems to have not mentioned this amount of phile: This also used to say? Hansbug:t T)
1 typeArr=Array[-200000..200000] ofLongint;2 var3 I,j,k,l,m,n:longint;4 A,b,c,d:arr;5 functionxxx (x:longint): Longint;6          begin7Exit (x* (X-1)Div 2);8          End;9 procedureSwapvarx,y:longint);Ten           varZ:longint; One           begin Az:=x;x:=y;y:=Z; -           End; - procedureSort (l,r:longint;varA,b:arr); the           varI,j,x,y:longint; -           begin -i:=l;j:=r;x:=b[(L+r)Div 2]; -                Repeat +                       whileB[i]<x DoInc (i); -                       whileB[j]>x DoDec (j); +                      ifI<=j Then A                         begin at swap (b[i],b[j]); - swap (a[i],a[j]); - Inc (I);d EC (j); -                         End; -                untilI>J; -                ifL<j Thensort (l,j,a,b); in                ifI<r Thensort (i,r,a,b); -           End; to begin + readln (n,m); -a[0]:=0; b[0]:=0; the       fori:=1  toN Do *          begin $ read (a[i]);Panax Notoginseng               ifA[i]>m Then -a[i]:=1 the               Else ifA[i]=m Thena[i]:=0 Elsea[i]:=-1; +b[i]:=b[i-1]+A[i]; A          End; the       forI:=nDownto 1  Dob[i+1]:=B[i]; +       fori:=n+1 Downto 1  Doa[i]:=i-1; -b[1]:=0; $Sort1, n+1, A, b); $       fori:=1  ton+1  DoA[i]:=a[i]MoD 2; -b[0]:=-Maxlongint; -j:=0; theb[n+2]:=maxlongint;l:=0; -       fori:=1  ton+2  DoWuyi          ifB[I]&LT;&GT;B[J] Then the             begin -Sort (j,i-1, b,a); Wuj:=i; -             End; Aboutj:=0; $Fillchar (C,sizeof (c),0); -Fillchar (d,sizeof (d),0); -       fori:=1  ton+2  Do -          begin A               if  not((B[i]=b[j]) and(A[i]=a[j])) Then +                  begin the                       ifJ<>0  Then ifa[j]=0  ThenC[b[j]]:=c[b[j]]+i-jElsed[b[j]]:=d[b[j]]+i-J; -j:=i; $                  End; the          End; the       fori:=-200000  to 200000  Dol:=l+c[i]*D[i]; the Writeln (l); the End. -                                    

1303: [CQOI2009] Median figure

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.