天天看点

CF899E

E. Segments Removal time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))

const int N=2e5+5;
int pre[N];
int nxt[N];
int col[N];
int num[N];
priority_queue<pair<int,int> >q,del;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,a,cnt=0,ans=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==col[cnt])num[cnt]++;
        else col[++cnt]=a,num[cnt]=1;
    }
    for(int i=1;i<=cnt;i++)pre[i]=i-1,nxt[i]=i+1,q.push(mp(num[i],-i));//建立链表,区间入队,-i是因为优先选择最左边的区间
    while(cnt)
    {
        while(!del.empty()&&del.top()==q.top())del.pop(),q.pop();//删除已经被合并的区间
        a=-q.top().second;
        q.pop();
        int t=pre[a],_t=nxt[a];
        pre[_t]=t,nxt[t]=_t;
        if(t&&col[t]==col[_t])//如果左右两个区间颜色一样,合并两个区间
        {
            del.push(mp(num[t],-t)),del.push(mp(num[_t],-_t));//把合并前的区间放进待删队列
            num[t]+=num[_t],nxt[t]=nxt[_t],pre[nxt[_t]]=t;
            q.push(mp(num[t],-t));
            cnt--;
        }
        cnt--;
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}