POJ 3670 Eating Together LIS最长递增子序列+二分查找 nlogn算法

原创 2011年01月21日 19:36:00

http://poj.org/problem?id=3670

 

Eating Together
Time Limit: 1000MS   Memory Limit: 65536K



Description

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

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

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

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

Input

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

Output

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

Sample Input

5
1
3
2
1
1

Sample Output

1
 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 3670 Eating Together(LIS+nlogn算法)

The cows are so very silly about their dinner partners. They have organized themselves into three gr...

最长递增子序列LIS(动态规划+二分查找)

/* * ===================================================================================== * * ...

最长递增子序列(LIS)的O(NlogN)打印算法

题目: 求一个一维数组arr[n]中的最长递增子序列的长度,如在序列1,5,8,3,6,7中,最长递增子序列长度为4 (即1,3,6,7)。 方法一:一般的DP方法(O(N^2)) 像LC...

POJ2533, 最长上升子序列(贪心+二分查找时间复杂度O(nlogn))

解题思路参考这位大神的博客:点击打开链接。 在这里,我介绍下我对该方法的理解。 使用这种方法是对动态规划方法的一种优化,在用动态规划求解时,求到第i个元素的最长上升子序列时,是在前i-1个数中寻找,比...

poj 3670 Eating Together LIS+二分

http://poj.org/problem?id=3670   Eating Together Time Limit: 1000MS   Memory Limit: 655...

hdu 3998 Sequence 贪心算法+最长递增子序列+二分查找(好题目)

题意:给定一个数组,求该数组中严格最长递增子序列的长度和相应的最长子序列的个数,在求最长子序列的有多少种时,数组中每一个元素只能使用一次 思路:1. 求最长递增子序列的长度时,我们使用二分查找的方法...

POJ 3670 Eating Together (①O(n)的dp,②最长字段和)

题目大意:找到队列中不符合非升(降)序趋势的编号个数,分别判断升序跟降序的个数,最后取最小。#include #include #include #include using namespac...

最长递增子序列LIS的O(nlogn)的求法

最长递增子序列(Longest Increasing Subsequence)是指n个数的序列的最长单调递增子序列。比如,A = [1,3,6,7,9,4,10,5,6]的LIS是1 3 6 7 9 ...

ZOJ2319 Beautiful People(贪心,最长递增子序列nlogn,LIS)

题目: Beautiful People Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge Th...

最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现

求最长递增(递减)子序列 LIS时间复杂度O(nlogn)的Java实现...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 3670 Eating Together LIS最长递增子序列+二分查找 nlogn算法
举报原因:
原因补充:

(最多只允许输入30个字)