Codeforces Round #452 (Div. 2)-E-Segments Removal(set)

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.

题意:给你一个长为n的序列,每次对当前序列进行一次操作,就是将当前序列中最长的一段各元素相等的子段删掉,若满足题意的最长子段不止一条,则删最左边的,问你将该序列删完要操作多少次?

题解:很明显的set应用,我们开两个set,一个存子段的长度和下标,另一个只存下标,然后每次将当前最长且在左边的元素给删掉,用另一个set来判断,删掉的子段左右两边的序列是否可以合成一个长序列(当然也可以用数组模拟,你开心就好)

#include<set>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
	int x,y;
	node(int a=0,int b=0){x=a;y=b;}
	bool operator <(const node &b) const
	{
		if(x==b.x)
			return y<b.y;
		return x>b.x;
	}
}now;
set<int>s1;set<int>:: iterator x,y;
set<node>s;set<node>:: iterator p;
int a[200005],n,ans,sum[200005];
int main(void)
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	s.insert(node(0,0));
	s.insert(node(0,n+1));
	s1.insert(0);s1.insert(n+1);
	for(int i=1;i<=n;i++)
	{
		if(a[i]==a[i-1])sum[i]=sum[i-1]+1;
		else sum[i]=1;
		if(a[i]!=a[i+1])
			s.insert(node(sum[i],i)),s1.insert(i);
	}
	while(1)
	{
		p=s.begin();now=*p;
		if(now.x==0) break;
		ans++;s.erase(p);
		x=y=s1.find(now.y);
		x--;y++;
		s1.erase(s1.find(now.y));
		if(a[*x]==a[*y])
		{
			s.erase(node(sum[*x],*x));
			s.erase(node(sum[*y],*y));
			sum[*y]+=sum[*x];
			s.insert(node(sum[*y],*y));
			s1.erase(x);
		}
	}
	printf("%d\n",ans);
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值