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