DiningCows

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8943 Accepted: 3687

Description

The cows are so very silly about their dinner partners. They haveorganized themselves into two groups (conveniently numbered 1 and 2) thatinsist upon dining together in order, with group 1 at the beginning of the lineand group 2 at the end. The trouble starts when they line up at the barn toenter the feeding area.

Each cow i carries with her a small card uponwhich is engraved Di (1 ≤ Di ≤ 2) indicating her dining groupmembership. 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 112222 or111122 where the cows' dining groups are sorted in ascending order by theirdinner cards. Rarely he might change cards so that only one group of cows isleft (e.g., 1111 or 222).

FJ is just as lazy as the next fellow. He's curious: what is theabsolute minimum 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+1describes cow i'sdining preference with a single integer: Di

Output

* Line 1: A single integer that is the minimum number of cardsFarmer John must change to assign the cows to eating groups as described.

SampleInput

7

2

1

1

1

2

2

1

SampleOutput

2

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]);
}

printf("%d\n",n-an1);
return 0;
}