EatingTogether

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6363 Accepted: 3081

Description

The cows are so very silly about their dinner partners. They haveorganized themselves into three groups (conveniently numbered 1, 2, and 3) thatinsist upon dining together. The trouble starts when they line up at the barnto enter the feeding area.

Each cow i carries with her a small card uponwhich is engraved Di (1 ≤ Di ≤ 3) indicating her dininggroup membership. The entire set of N (1 ≤ N ≤ 30,000) cows has lined up for dinnerbut it's easy for anyone to see that they are not grouped by theirdinner-partner cards.

FJ's job is not so difficult. He just walks down the line of cowschanging their dinner partner assignment by marking out the old number andwriting in a new one. By doing so, he creates groups of cows like 111222333 or333222111 where the cows' dining groups are sorted in either ascending ordescending order by their dinner cards.

FJ is just as lazy as the next fellow. He's curious: what is theabsolute mminimum number of cards he must change to create a proper grouping ofdining partners? He must only change card numbers and must not rearrange thecows standing in line.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i describes the i-th cow's current dining groupwith a single integer: Di

Output

* Line 1: A single integer representing the minimum number ofchanges that must be made so that the final sequence of cows is sorted ineither ascending or descending order

SampleInput

5

1

3

2

1

1

SampleOutput

1

Source

`#include <cstdio>  #include <algorithm>  using namespace std;  #define N 30005  int dp1[N],dp2[N],a[N],ans1[N],ans2[N];  int main()  {      int n;      scanf("%d",&n);      for(int i=0;i<n;i++)      {          scanf("%d",&a[i]);      }      int len =0,an1=0;          for(int i=0;i<n;i++) //最长不下降子序列upper实现     {        int pos=upper_bound(ans1,ans1+len,a[i])-ans1;      if(pos<len) ans1[pos]=a[i];        else    ans1[len++]=a[i];      dp1[i]=len;          an1=max(an1,dp1[i]);    }      len=0;       int an2=0;    for(int i=n-1;i>=0;i--)      {        int pos=upper_bound(ans2,ans2+len,a[i])-ans2;       if(pos<len) ans2[pos]=a[i];        else    ans2[len++]=a[i];         dp2[i]=len;          an2=max(an2,dp2[i]);    }    int ans;     ans=max(an1,an2);     printf("%d\n",n-ans);     return 0;  }`