[bzoj1293] [SCOI2009] Birthday Gift

Source: Internet
Author: User

Topic Stamp Here

The problem is to be blind. Feel my method is more troublesome ah ...

First put all the positions in order in the queue, and then sweep back, with the array to record the number of each color between the team head and the tail of the team, so that when the tail of the pointer to move back, check the team the first pointer refers to the place of the color of the beads is more than 1, if more than one can move the pointer forward Knowing that the color of the team's head pointer is only one in the current queue ensures that at least one minimum length is met for each color in the case where the tail pointer is determined. When you sweep, update ans.

As for the sort I use the heap, because the data is ordered, so directly to build a heap of k elements directly to do it, time complexity nlogk

Code ugly Horrible ... Normally it should be able to press down One-third ... Have time to reconstruct ...

1  Programj01;2 typexx=Record3 C,x:longint;4           End;5 varQ,next:Array[1..1010000] ofLongint;6Head,num,now:Array[1.. -] ofLongint;7LC,LN:Array[0..1010000] ofLongint;8 N,k,h,t,tt,i,j,a,b,m,ans:longint;9HeapArray[1.. -] ofxx;TenSumArray[1.. -] ofLongint; OneZz:Array[1..1010000] ofLongint; A  - procedureAdd (a,b:longint); - begin the Inc (TT); -q[tt]:=b; -next[tt]:=Head[a]; -head[a]:=tt; + End; -  + functionmin (a,b:longint): Longint; A begin at   ifA<b ThenExit (a)Elseexit (b); - End; -  - procedureSwapvara,b:xx); - varc:xx; - begin inc:=a;a:=b;b:=C; - End; to  + procedurebuild (i:longint); - begin the    whileI>1  Do *   begin $     ifHeap[i].x>heap[iDiv 2].x ThenSwap (heap[i],heap[iDiv 2])Elseexit;Panax NotoginsengI:=iDiv 2; -   End; the End; +  A procedureUpdate (i:longint); the begin +    whilei*2<=m Do -   begin $i:=i*2; $     if(heap[i].x1].x) and(i+1&LT;=M) ThenInc (i); -     ifHeap[i].x>heap[iDiv 2].x ThenSwap (heap[i],heap[iDiv 2])Elseexit; -   End; the End; - Wuyi begin the readln (n,k); -tt:=0; WuFillchar (Head,sizeof (head),0); -    fori:=1  toK Do About   begin $ read (num[i]); -      forj:=1  toNum[i] Do -     begin - read (b); A Add (i,b); +     End; the   End; -    fori:=1  toK Do $   begin thej:=Head[i]; theheap[i].c:=i; theheap[i].x:=Q[j]; the build (i); -now[i]:=J; in   End; them:=K; the    fori:=1  toN Do About   begin thelc[i]:=heap[1].c; theln[i]:=heap[1].x; theSwap (heap[1],heap[m]); + Dec (m); -Update1); thenow[lc[i]]:=Next[now[lc[i]];Bayi     ifNow[lc[i]]<>0  Then the     begin the Inc (M); -heap[m].x:=Q[now[lc[i]]; - build (m); the     End; the   End; theh:=1; theFillchar (sum,sizeof (sum),0); -t:=1; theans:=Maxlongint; thesum[lc[1]]:=1; the    fori:=2  toN Do94   begin the Inc (Sum[lc[i]); the     ifsum[lc[i]]=1  ThenInc (T); the      whileSum[lc[h]]>1  Do98     begin About Dec (sum[lc[h]]); - Inc (h);101     End;102     ifT=k ThenAns:=min (ans,ln[h]-ln[i]);103   End;104 writeln (ans); the End.
View Code

[bzoj1293] [SCOI2009] Birthday Gift

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.