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.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains a sequence a1, a2, …, an (1 ≤ ai ≤ 109) — Vasya’s array.
Output
Print the number of operations Vasya should make to remove all elements from the array.
Examples
Input
4
2 5 5 2
Output
2
Input
5
6 3 4 1 5
Output
5
Input
8
4 4 4 2 2 100 100 100
Output
3
Input
6
10 10 50 10 50 50
Output
4
Note
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.
题意:每次选取数组中连续出现的长度最大的删去,问最少多少步;
思路:用链表的思路去实现,
将每一个连续的分为一个部分,当删去中间一部分时,注意判断前后是否一样;
用两个优先级队列解决
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
ll quickpow(ll a, ll b) {
ll ans = 1;
while (b > 0) {
if (b % 2)ans = ans * a;
b = b / 2;
a = a * a;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
priority_queue< pii >q, dt;
int n;
int a[maxn];
int front[maxn], nxt[maxn];
int sum[maxn];
int main()
{
ios::sync_with_stdio(false);
cin >> n;
int i, j;
int tmp;
int now = 0;
int tot = 0;
for (i = 1; i <= n; i++) {
cin >> tmp;
if (tmp == a[now]) {
sum[now]++;
}
else {
now++;
a[now] = tmp;
sum[now] = 1;
}
}
for (i = 1; i <= now; i++) {
front[i] = i - 1;
nxt[i] = i + 1;
q.push(make_pair(sum[i], -i));
}
for(i=0;i<now;i++){
while (!dt.empty()) {
if (q.top() == dt.top()) {
q.pop();
dt.pop();
continue;
}
else break;
}
tmp = -(q.top().second);
q.pop();
int frnt1 = front[tmp];
int nxx1 = nxt[tmp];
nxt[frnt1] = nxx1;
front[nxx1] = frnt1;
if (a[frnt1] == a[nxx1]) {
dt.push(make_pair(sum[frnt1],-frnt1));
dt.push(make_pair(sum[nxx1], -nxx1));
sum[frnt1] += sum[nxx1];
nxt[frnt1] = nxt[nxx1];
front[nxt[nxx1]] = frnt1;
q.push(make_pair(sum[frnt1], -frnt1));
i++;
}
tot++;
}
cout << tot << endl;
}