A11379 | Segments Removal
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains a single integer $n$ ( $1<=n<=200000$ ) — the length of the array.
The second line contains a sequence $a_{1},a_{2},...,a_{n}$ ( $1<=a_{i}<=10^{9}$ ) — Vasya's array.
The second line contains a sequence $a_{1},a_{2},...,a_{n}$ ( $1<=a_{i}<=10^{9}$ ) — Vasya's array.
输出格式
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted