The problem, once introduced six-dimensional DP, finally read the puzzle.
Dawned... (A[i]>=25 and a[i]<=32) are used to compress the same a[i] and compress them into a single value.
Because there are only two possibilities of taking out a book, (1) put it to the front, (2) next to the book with the same number as it, then we can add the restriction, we can introduce the state transfer equation (PS: Each time the book must be taken out of a regiment, otherwise does not change the original state)
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#definell Long Longusing namespacestd;ll n,k,a[10005],h[100005],x,ans,flag[100005],sum[10005],f[2][ the][Ten][1<<8],tot,all,p[10005];intMain () {scanf ("%lld%lld",&n,&k); for(intI=1; i<=n; i++) scanf ("%lld", a+i), a[i]-= -; for(intI=1; i<=n; i++) { if(a[i]!=a[i-1]) h[++tot]=A[i]; Sum[tot]++; } for(intI=tot; I i--){ if(Flag[h[i]]) p[i]=1; Flag[h[i]]=1; } memset (f[0],127,sizeof(f[0])); f[0][0][0][0]=0; All=(1<<8)-1; for(intI=1; i<=tot; i++) {x=x^1; memset (F[x],127,sizeof(F[x])); for(intj=0; j<=k; J + +) for(intw=0; w<=8; w++) for(intm=0; m<=all; m++) { if(f[x^1][j][w][m]>n)Continue; F[x][j][h[i]][m| (1<< (h[i]-1))]=min (F[x][j][h[i]/*the last one at the moment is h[i]*/][m| (1<< (h[i]-1))],f[x^1][j][w][m]+ (h[i]!=w));//the current state is equal to the previous state + (determines whether the scanned bit is equal to the previous last) if(j+sum[i]>k)Continue; F[x][j+sum[i]][w][m| (1<< (H[i]/*all of the same values are merged after the*/-1)/*h[i]-1 Indicates whether H[i] has been taken, with MOR to indicate the current state*/)]=min (f[x][j+sum[i]][w][m| (1<< (h[i]-1))],f[x^1][j][w][m]+! (m& (1<< (h[i]-1)))); if(P[i]) f[x][j+sum[i]/*changed to J+sum[i] times after the state*/][w][m]=min (F[x][j+sum[i]/*changed to J+sum[i] times after the state*/][w][m],f[x^1][j][w][m]); } } //each time x is manipulated to create a scrolling array, that is, the state of this time is only related to the last stateans=124278904761894269; for(intI=0; i<=k; i++) for(intw=0; w<=8; w++) for(intm=0; m<=all; m++) ans=min (ans,f[x][i][w][m]); printf ("%lld\n", ans); return 0;}
Help Bsny (state compression + 4-D dp+ scrolling array)