Sequence (Bzoj 1367)

Source: Internet
Author: User

Descriptioninputoutput an integer rsample Input7
9
4
8
20
14
15
-Sample Output -HINT

The z-sequence is 6,7,8,13,14,15,18.
R=13

/*thinking is twisted (I can't think of it anyway).  First think about not falling: Consider a sequence of positive orders, Z[i]=t[i] Consider a sequence of reverse order, z[i]=x (X is the median of the inverse column) since that is so we can divide the whole serialization into several paragraphs in reverse order, for each segment to find the median (the positive sequence can be regarded as the number of sequential reverse).  Maintain the median number with the left-leaning tree, by always keeping the number of heaps in the heap no greater than half the number of original numbers.    As for the ascending, the original sequence is t[i]-=i.      PS: The relationship between the root[i in the puzzle] and the bottom of the heap I was blindfolded, so here's the point.      The code builds n heaps, but merges and eventually becomes tot. Root[i] Indicates which element is the top of the heap of the first (merged) heap. */#include<cstdio>#include<iostream>#include<cstdlib>#defineN 1000010using namespacestd;intT[n],root[n],l[n],r[n],num[n],cnt[n],n,tot;structnode{intl,r,dis,w;}; Node Heap[n];intMergeintAintb) {    if(a==0|| b==0)returnA +C; if(heap[a].w<heap[b].w) Swap (A, b); HEAP[A].R=merge (HEAP[A].R,B); if(heap[heap[a].r].dis>Heap[heap[a].l].dis) Swap (HEAP[A].R,HEAP[A].L); Heap[a].dis=heap[heap[a].r].dis+1; returnA;}intPopinta) {    returnmerge (HEAP[A].L,HEAP[A].R);}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", &t[i]), t[i]-=i;  for(intI=1; i<=n;i++){        ++tot; L[tot]=r[tot]=i; Num[tot]=cnt[tot]=1; Root[tot]=i; Heap[i].dis=heap[i].l=heap[i].r=0; HEAP[I].W=T[i];  while(tot>1&&heap[root[tot]].w1]].w) {--tot; Root[tot]=merge (root[tot],root[tot+1]); Num[tot]+=num[tot+1],cnt[tot]+=cnt[tot+1],r[tot]=r[tot+1];  for(; cnt[tot]*2>num[tot]+1;--Cnt[tot]) Root[tot]=pop (Root[tot]); }    }    Long Longans=0;  for(intI=1; i<=tot;i++)         for(intj=l[i],w=heap[root[i]].w;j<=r[i];j++) ans+=abs (t[j]-W); cout<<ans; return 0;} 

Sequence (Bzoj 1367)

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.