BZOJ1786: [Ahoi2008]pair pairing/1831: [AHOI2008] Reverse order

Source: Internet
Author: User

The two questions are the same.

It can be found that 1 turns into a number that is monotonous and does not drop.

Record the number of the original reverse order.

The number of reverse pairs generated by each value per point is preprocessed, and then the DP is transferred.

#include <cstring>#include<iostream>#include<cstdio>#include<map>#include<cmath>#include<algorithm>#defineRep (i,l,r) for (int i=l;i<=r;i++)#defineDown (i,l,r) for (int i=l;i>=r;i--)#defineCLR (x, y) memset (x,y,sizeof (×))#defineLow (x) (x& (×))#defineMAXN 10050#defineINF 2000000000#defineMM 1000000007using namespacestd;intf[maxn][ the],a[maxn],t[maxn],pos[maxn],c[maxn][ the];intNow,n,k,cnt,ans;intRead () {intx=0, f=1;CharCh=GetChar ();  while(!isdigit (CH)) {if(ch=='-') f=-1; Ch=GetChar ();}  while(IsDigit (CH)) {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}voidAddintXinty) {     while(x<=k) {T[x]=t[x]+y; X+=Low (x); }}intAskintx) {    intans=0;  while(x) {ans+=T[x]; X-=Low (x); }    returnans;}intMain () {n=read (); k=read (); Rep (I,1, N) {A[i]=read (); if(a[i]!=-1) {ans+=ask (k)-ask (A[i]); Add (A[i],1); }        Else{pos[++cnt]=i; Rep (J,1, k) C[i][j]+=ask (k)-Ask (j); }} clr (T,0); Down (I,n,1){        if(a[i]!=-1) Add (A[i],1); Else{Rep (J,1, K) C[i][j]+=ask (J-1); }} rep (I,1, CNT) Rep (J,0, k) f[i][j]=inf; Rep (I,1, CNT) {        intnow=Pos[i]; Rep (J,1, k) F[i][j]=min (f[i][j-1],f[i-1][j]+C[now][j]); } printf ("%d\n", f[cnt][k]+ans); return 0;} 

BZOJ1786: [Ahoi2008]pair pairing/1831: [AHOI2008] Reverse pair

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.