Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 8943
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.
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1describes cow i'sdining preference with a single integer: Di
* Line 1: A single integer that is the minimum number of cardsFarmer John must change to assign the cows to eating groups as described.
USACO 2008 February Bronze