CF1479B1.Painting the Array I

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The only difference between the two versions is that this version asks the maximal possible answer.

Homer likes arrays a lot. Today he is painting an array a1,a2,…,ana_1, a_2, \dots, a_n with two kinds of colors, white and black. A painting assignment for a1,a2,…,ana_1, a_2, \dots, a_n is described by an array b1,b2,…,bnb_1, b_2, \dots, b_n that bib_i indicates the color of aia_i (00 for white and 11 for black).

According to a painting assignment b1,b2,…,bnb_1, b_2, \dots, b_n, the array aa 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 aa and a(1)a^{(1)} is the sub-sequence of all black elements in aa. 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,…,ckc_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 cc. For example, the number of segments in [1,1,2,2,3,3,3,2][1,1,2,2,3,3,3,2] is 44, 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 00.

Homer wants to find a painting assignment bb, 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 large as possible. Find this number.

两个版本唯一的区别在于,本版本要求的是最大可能的答案。

霍默非常喜欢数组。今天,他正在用两种颜色(白色和黑色)给一个数组 a1,a2,…,ana_1, a_2, \dots, a_n 上色。对数组 a1,a2,…,ana_1, a_2, \dots, a_n 的一种上色方案由一个数组 b1,b2,…,bnb_1, b_2, \dots, b_n 描述,其中 bib_i 表示 aia_i 的颜色(00 表示白色,11 表示黑色)。

根据上色方案 b1,b2,…,bnb_1, b_2, \dots, b_n,原数组 aa 被划分为两个新数组 a(0)a^{(0)} 和 a(1)a^{(1)}:其中 a(0)a^{(0)} 是 aa 中所有白色元素构成的子序列,a(1)a^{(1)} 是 aa 中所有黑色元素构成的子序列。例如,若 a=[1,2,3,4,5,6]a = [1,2,3,4,5,6] 且 b=[0,1,0,1,0,0]b = [0,1,0,1,0,0],则 a(0)=[1,3,5,6]a^{(0)} = [1,3,5,6],a(1)=[2,4]a^{(1)} = [2,4]。

一个数组 c1,c2,…,ckc_1, c_2, \dots, c_k 的段数(记为 seg(c)\mathit{seg}(c))定义为:将 cc 中所有相邻且值相等的元素合并后,剩余元素的个数。例如,数组 [1,1,2,2,3,3,3,2][1,1,2,2,3,3,3,2] 的段数为 44,因为合并相邻相同元素后得到 [1,2,3,2][1,2,3,2]。特别地,空数组的段数为 00。

霍默希望找到一个上色方案 bb,使得 a(0)a^{(0)} 和 a(1)a^{(1)} 的段数之和(即 seg(a(0))+seg(a(1))\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)}))尽可能大。请找出这个最大值。

输入格式

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

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \leq a_i \leq n).

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \leq a_i \leq n)。

输出格式

Output a single integer, indicating the maximal possible total number of segments.

输出一个整数,表示线段总数的最大可能值。

输入输出样例

  • 输入#1

    7
    1 1 2 2 3 3 3

    输出#1

    6
  • 输入#2

    7
    1 2 3 4 5 6 7

    输出#2

    7

说明/提示

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

In the second example, we can choose a(0)=[1,2,3,4,5,6,7]a^{(0)} = [1,2,3,4,5,6,7] and a(1)a^{(1)} is empty. We can see that seg(a(0))=7\mathit{seg}(a^{(0)}) = 7 and seg(a(1))=0\mathit{seg}(a^{(1)}) = 0. So the answer is 7+0=77+0 = 7.

在第一个例子中,我们可以选择 a(0)=[1,2,3,3]a^{(0)} = [1,2,3,3]、a(1)=[1,2,3]a^{(1)} = [1,2,3],且 seg(a(0))=seg(a(1))=3\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3。因此答案为 3+3=63+3 = 6。

在第二个例子中,我们可以选择 a(0)=[1,2,3,4,5,6,7]a^{(0)} = [1,2,3,4,5,6,7],而 a(1)a^{(1)} 为空数组。可见 seg(a(0))=7\mathit{seg}(a^{(0)}) = 7 且 seg(a(1))=0\mathit{seg}(a^{(1)}) = 0。因此答案为 7+0=77+0 = 7。

输入解题思路,AI测评打分。不知道怎么写?

首页