CF899E.Segments Removal
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Vasya has an array of integers of length n.
Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2].
Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
瓦西娅有一个长度为 n 的整数数组。
瓦西娅对数组执行如下操作:在每一步中,他找出最长的连续相等整数段(若存在多个这样的段,则取最左边的一个)并将其删除。例如,若瓦西娅的数组为 [13, 13, 7, 7, 7, 2, 2, 2],则经过一次操作后变为 [13, 13, 2, 2, 2]。
请计算瓦西娅需要执行多少次操作才能使数组变为空(即所有元素均被删除)。
输入格式
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains a sequence _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 109) — Vasya's array.
第一行包含一个整数 n(1≤n≤200000)—— 数组的长度。
第二行包含一个序列 a1,a2,...,an(1≤ai≤109)—— Vasya 的数组。
输出格式
Print the number of operations Vasya should make to remove all elements from the array.
输出瓦西亚为从数组中删除所有元素所需执行的操作次数。
输入输出样例
输入#1
4 2 5 5 2
输出#1
2
输入#2
5 6 3 4 1 5
输出#2
5
输入#3
8 4 4 4 2 2 100 100 100
输出#3
3
输入#4
6 10 10 50 10 50 50
输出#4
4
说明/提示
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty.
In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array.
In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2.
In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
在第一个例子中,瓦西娅首先移除位于第二和第三位置的两个数字 5,数组变为 [2, 2]。在第二次操作中,瓦西娅移除位于第一和第二位置的两个数字 2,之后数组变为空。
在第二个例子中,瓦西娅需要执行五次操作才能使数组变为空。在每次操作中,他均移除数组的第一个元素。
在第三个例子中,瓦西娅需要三次操作:第一次操作中移除所有数字 4,第二次操作中移除所有数字 100,第三次操作中移除所有数字 2。
在第四个例子中,第一次操作中瓦西娅移除前两个数字 10,之后数组变为 [50, 10, 50, 50];第二次操作中,瓦西娅移除最右侧的两个数字 50,使数组变为 [50, 10];第三次操作中,他移除剩余的数字 50,之后数组变为 [10];最后一次(即第四次)操作中,他移除唯一剩下的数字 10,之后数组变为空。
输入解题思路,AI测评打分。不知道怎么写?