设up[i][j]为第i位升序为j的最小修改数,down为降序
#include<iostream>
#include<stdio.h>
using namespace std;
int n,a[30005],up[30005][4],down[30005][4];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
up[i][3]=min(up[i-1][1],min(up[i-1][2],up[i-1][3]))+1;
up[i][2]=min(up[i-1][1],up[i-1][2])+1;
up[i][1]=up[i-1][1]+1;
up[i][a[i]]--;
down[i][1]=min(down[i-1][1],min(down[i-1][2],down[i-1][3]))+1;
down[i][2]=min(down[i-1][2],down[i-1][3])+1;
down[i][3]=down[i-1][3]+1;
down[i][a[i]]--;
}
int ans=up[n][1];
for(int i=1;i<=3;i++)
ans=min(ans,min(up[n][i],down[n][i]));
printf("%d\n",ans);
return 0;
}