Codeforces Round #452 (Div. 2)

来源:互联网 发布:什么叫网络级防火墙 编辑:程序博客网 时间:2024/06/08 15:23

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
42 5 5 2
output
2
input
56 3 4 1 5
output
5
input
84 4 4 2 2 100 100 100
output
3
input
610 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.


题意:

每次找连续最长的序列(相等取左),并把它去掉。输出要做几次操作,变成空


POINT:

把每个连续的序列分号。从1 2 3标号。然后用链表连接。

全部放在优先队列里。

把最大的去掉之后。连接他的左边和右边。

判断他们能不能合并,可以的话,把他们从链表里删除,连接一个新值即他们两个的和,且他的l为左边的l。

给每个序列flag记录有么有被删了,被删了就不用ans++了。




#include <iostream>#include <stdio.h>#include <math.h>#include <queue>#include <algorithm>using namespace std;#define  LL long longconst int maxn = 500000+444;int flag[maxn],a[maxn];int num[maxn],pre[maxn],nxt[maxn];struct node{int id,l;int kind;bool operator < (const node &a) const{if(num[id]==num[a.id]){return l>a.l;}return num[id]<num[a.id];}}b[maxn];int cnt=0;int main(){priority_queue<node> q;int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}int x=0;a[n+1]=-1;for(int i=1;i<=n;i++){x++;if(a[i]!=a[i+1]){++cnt;b[cnt].id=cnt;b[cnt].kind=a[i];num[cnt]=x;x=0;}}for(int i=1;i<=cnt;i++){b[i].l=i;pre[i]=i-1;nxt[i]=i+1;q.push(b[i]);}nxt[cnt]=0;int ans=0;while(!q.empty()){node now=q.top();q.pop();if(flag[now.id]) continue;ans++;int p=pre[now.id];int nt=nxt[now.id];if(p==0||nt==0||b[p].kind!=b[nt].kind){nxt[p]=nt;pre[nt]=p;continue;}++cnt;flag[p]=flag[nt]=1;b[cnt].id=cnt;b[cnt].kind=b[p].kind;b[cnt].l=b[p].l;num[cnt]=num[p]+num[nt];q.push(b[cnt]);nxt[pre[p]]=cnt;pre[nxt[nt]]=cnt;nxt[cnt]=nxt[nt];pre[cnt]=pre[p];}printf("%d\n",ans);return 0;}


原创粉丝点击