Type: RemoteJudge 1000ms 256MiB

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 的唯一区别是本题询问最小可能解.

给定一个数组 aa, 你将将 aia_i 染为 bib_i 色, 其中 bb 是由你指定的一个 01 数组. 将 aa 数组中被染成 0 色的数字取出来并依在 aa 中出现的顺序排列, 组成数组 a(0)a^{(0)}. 同理, 将 aa 数组中被染成 1 色的数字取出来并依在 aa 中出现的顺序排列, 组成数组 a(1)a^{(1)}. 我们定义 seg(c)seg(c) 是一个正整数, 其中 cc 是一个数组, seg(c)seg(c) 的值为在我们将 cc 中相邻的所有相同元素合并后, cc 数组的大小. 例如, seg([1,1,4,5,1,4])=[1,4,5,1,4]=5seg([1, 1, 4, 5, 1, 4]) = |[1, 4, 5, 1, 4]|=5. 最小化 seg(a(0))+seg(a(1))seg(a^{(0)})+seg(a^{(1)}).

输入格式

第一行包括一个正整数 n(1n105)n(1\leq n\leq 10^5).

第二行包括 nn 个正整数 a1,a2,,an(1ain)a_1, a_2, \cdots,a_n(1\leq a_i\leq n).

输出格式

仅输出一个正整数, 代表最小的 seg(a(0))+seg(a(1))seg(a^{(0)})+seg(a^{(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 a1,a2,,an a_1, a_2, \dots, a_n with two kinds of colors, white and black. A painting assignment for a1,a2,,an a_1, a_2, \dots, a_n is described by an array b1,b2,,bn b_1, b_2, \dots, b_n that bi b_i indicates the color of ai a_i ( 0 0 for white and 1 1 for black).

According to a painting assignment b1,b2,,bn b_1, b_2, \dots, b_n , the array a a is split into two new arrays a(0) a^{(0)} and a(1) a^{(1)} , where a(0) a^{(0)} is the sub-sequence of all white elements in a a and a(1) a^{(1)} is the sub-sequence of all black elements in a a . For example, if a=[1,2,3,4,5,6] a = [1,2,3,4,5,6] and b=[0,1,0,1,0,0] b = [0,1,0,1,0,0] , then a(0)=[1,3,5,6] a^{(0)} = [1,3,5,6] and a(1)=[2,4] a^{(1)} = [2,4] .

The number of segments in an array c1,c2,,ck c_1, c_2, \dots, c_k , denoted seg(c) \mathit{seg}(c) , is the number of elements if we merge all adjacent elements with the same value in c c . For example, the number of segments in [1,1,2,2,3,3,3,2] [1,1,2,2,3,3,3,2] is 4 4 , because the array will become [1,2,3,2] [1,2,3,2] after merging adjacent elements with the same value. Especially, the number of segments in an empty array is 0 0 .

Homer wants to find a painting assignment b b , according to which the number of segments in both a(0) a^{(0)} and a(1) a^{(1)} , i.e. seg(a(0))+seg(a(1)) \mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)}) , is as small as possible. Find this number.

输入格式

The first line contains an integer n n ( 1n105 1 \leq n \leq 10^5 ).

The second line contains n n integers a1,a2,,an a_1, a_2, \dots, a_n ( 1ain 1 \leq a_i \leq n ).

输出格式

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 a(0)=[1,1,2,2] a^{(0)} = [1,1,2,2] , a(1)=[2,3] a^{(1)} = [2,3] and seg(a(0))=seg(a(1))=2 \mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 2 . So the answer is 2+2=4 2+2 = 4 .

In the second example, we can choose a(0)=[1,1,1,1] a^{(0)} = [1,1,1,1] , a(1)=[2,2,2] a^{(1)} = [2,2,2] and seg(a(0))=seg(a(1))=1 \mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 1 . So the answer is 1+1=2 1+1 = 2 .

北辰OI俱乐部算法提高班:贪心专题

Not Claimed
Status
Done
Problem
15
Open Since
2023-11-25 0:00
Deadline
2024-12-26 23:59
Extension
24 hour(s)