bzoj1609[usaco2008 feb]eating together a troublesome dinner
Test instructions
A sequence consists of only three 1﹑2﹑3. The minimum number of changes to make it into a non-descending sequence or a non-ascending sequence. Sequence size ≤30000
Exercises
Dp. F[I][J] indicates that the number of I is being considered and the previous number is J. The minimum number of changes in a sequence of non-descending sequences is:
F[i][j]=min (f[i+1][k]+1,k∈[j,3]), a[i]<j
Min (f[i+1][a[i]],f[i+1][k]+1,k∈[j,3] and k!=a[i]) a[i]>=j
Beg not to rise likewise.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 500005 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineDec (i,j,k) for (int i=j;i>=k;i--)7 #defineINF 0X3FFFFFFF8 using namespacestd;9 Ten intn,a[maxn],x[5],y[5],ans; One intMain () { Ascanf"%d", &n); Inc (I,1, N) scanf ("%d", &a[i]); memset (x,0,sizeof(x)); -Dec (I,n,1){ -memset (Y,0,sizeof(y)); theInc (J,1,3)if(a[i]<j) { -Y[j]=inf; Inc (K,J,3) Y[j]=min (y[j],x[k]+1); -}Else{ -Y[j]=x[a[i]]; Inc (K,J,3)if(K!=a[i]) y[j]=min (y[j],x[k]+1); + } - swap (x, y); + } AAns=inf; Inc (I,1,3) Ans=min (Ans,x[i]); memset (x,0,sizeof(x)); atDec (I,n,1){ -memset (Y,0,sizeof(y)); -Inc (J,1,3)if(a[i]>j) { -Y[j]=inf; Dec (k,j,1) Y[j]=min (y[j],x[k]+1); -}Else{ -Y[j]=x[a[i]]; Dec (k,j,1)if(K!=a[i]) y[j]=min (y[j],x[k]+1); in } - swap (x, y); to } +Inc (I,1,3) Ans=min (Ans,x[i]); printf"%d", ans);return 0; -}
20160729
bzoj1609[usaco2008 feb]eating together a troublesome dinner