bzoj1609[usaco2008 feb]eating together a troublesome dinner

Source: Internet
Author: User

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

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.