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
USACO 2008 February Silver
算法分析:
题意:给你一堆1,2,3序列,改动最小的数字,让它成为升序或者降序。
正反跑最长不下降子序列,求出升序an1,降序an2,谁最大,谁就改动的次数最小,n减去即可,这里用的upper_bound,用普通竟然超时。
代码实现: