Painting the Array II
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
题面翻译
题意
本题与 CF1480D1 的唯一区别是本题询问最小可能解.
给定一个数组 , 你将将 染为 色, 其中 是由你指定的一个 01 数组. 将 数组中被染成 0 色的数字取出来并依在 中出现的顺序排列, 组成数组 . 同理, 将 数组中被染成 1 色的数字取出来并依在 中出现的顺序排列, 组成数组 . 我们定义 是一个正整数, 其中 是一个数组, 的值为在我们将 中相邻的所有相同元素合并后, 数组的大小. 例如, . 最小化 .
输入格式
第一行包括一个正整数 .
第二行包括 个正整数 .
输出格式
仅输出一个正整数, 代表最小的 .
题目描述
The only difference between the two versions is that this version asks the minimal possible answer.
Homer likes arrays a lot. Today he is painting an array with two kinds of colors, white and black. A painting assignment for is described by an array that indicates the color of ( for white and for black).
According to a painting assignment , the array is split into two new arrays and , where is the sub-sequence of all white elements in and is the sub-sequence of all black elements in . For example, if and , then and .
The number of segments in an array , denoted , is the number of elements if we merge all adjacent elements with the same value in . For example, the number of segments in is , because the array will become after merging adjacent elements with the same value. Especially, the number of segments in an empty array is .
Homer wants to find a painting assignment , according to which the number of segments in both and , i.e. , is as small as possible. Find this number.
输入格式
The first line contains an integer ( ).
The second line contains integers ( ).
输出格式
Output a single integer, indicating the minimal possible total number of segments.
样例 #1
样例输入 #1
6
1 2 3 1 2 2
样例输出 #1
4
样例 #2
样例输入 #2
7
1 2 1 2 1 2 1
样例输出 #2
2
提示
In the first example, we can choose , and . So the answer is .
In the second example, we can choose , and . So the answer is .
北辰OI俱乐部算法提高班:贪心专题
- Status
- Done
- Problem
- 15
- Open Since
- 2023-11-25 0:00
- Deadline
- 2024-12-26 23:59
- Extension
- 24 hour(s)