codeforces 962 D. Merge Equals

 

D. Merge Equals

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value xx that occurs in the array 22 or more times. Take the first two occurrences of xx in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, 2⋅x2⋅x).

Determine how the array will look after described operations are performed.

For example, consider the given array looks like [3,4,1,2,2,1,1][3,4,1,2,2,1,1]. It will be changed in the following way: [3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1][3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].

If the given array is look like [1,1,3,1,1][1,1,3,1,1] it will be changed in the following way: [1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4][1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].

Input

The first line contains a single integer nn (2≤n≤1500002≤n≤150000) — the number of elements in the array.

The second line contains a sequence from nn elements a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.

Output

In the first line print an integer kk — the number of elements in the array after all the performed operations. In the second line print kk integers — the elements of the array after all the performed operations.

Examples

input

Copy

7
3 4 1 2 2 1 1

output

Copy

4
3 8 2 1 

input

Copy

5
1 1 3 1 1

output

Copy

2
3 4 

input

Copy

5
10 40 20 50 30

output

Copy

5
10 40 20 50 30 

Note

The first two examples were considered in the statement.

In the third example all integers in the given array are distinct, so it will not change.

 

这一题虽然数是1->1e9  但只有150000个数,我们可以用优先队列搞。

先把数值存在first   位置存在second,小根堆

如果堆第一个和第二个相同就合并(同时pop掉),成一个新的 值为一二和,位置是2的位置(因为要再2的右边,2没了所以直接在2的位置就行)

如果不相同,说明第一个以后不会再合并了,因为合并只能把元素变大,后面会越来越大的,所以把这个元素放到prq输出优先队列里,这个优先队列按位置优先。

最后输出就行了。注意   size()函数默认是 unsigned int  32位,输出是不能直接%64;

 

#include<bits/stdc++.h>
using namespace std;
const int M = 150000+100;
typedef __int64 ll;
int main()
{
	ll n;
	cin>>n;
	ll w;
	priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > >que,prq;
	for(ll i=1;i<=n;i++)
	{
		scanf("%I64d",&w);
		que.push(make_pair(w,i));
	}
	pair<ll,ll>x,y;
	x=que.top();
	que.pop();
	while(!que.empty())
	{
		y=que.top();
	//	printf("%d   %d\n",y.first,y.second);
		if(x.first!=y.first)
		{
			prq.push(make_pair(x.second,x.first));
			x=y;
			que.pop();
		}
		else
		{
			que.pop();
			que.push(make_pair(y.first*2,y.second));
			x=que.top();
			que.pop();
		}
	}
	prq.push(make_pair(x.second,x.first));//最后剩的x不要忘了加;
	ll q=prq.size();
    printf("%I64d\n%I64d",q,prq.top().second);   //2
	//printf("%I64d\n%I64d",prq.size(),prq.top().second);//1   这样错的  .size()默认u32int  会错。。
	prq.pop();
	while(!prq.empty())
	{
		printf(" %I64d",prq.top().second);
		prq.pop();
	}
	puts("");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值