Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6363
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.
* 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
* 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
USACO 2008 February Silver