1. 程式人生 > >codeforces 962D. Merge Equals(STL)

codeforces 962D. Merge Equals(STL)

                                                                                                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.

一、原題地址

點我傳送

二、大致題意

給出n個數,然後每次操作都會把陣列中最小的兩個數取出來,如果這兩個數相同,則把左邊那個從陣列中刪除,然後把右邊那個置為這兩個數的和。詢問的是最後這個陣列會變成什麼樣。

三、思路

STL水題

四、程式碼

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <iterator>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;


int n;
LL a[200000];
map<LL, set<int>>mmp;
void read()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		mmp[a[i]].insert(i);
	}
}
void solve()
{
	map<LL, set<int>>::iterator it = mmp.begin();
	for (; it != mmp.end(); it++)
	{
		while (it->second.size() >= 2)
		{
			set<int>::iterator iit = it->second.begin(),t;
			t = iit; t++;
			a[*iit] = -inf;
			a[*t] = 2 * it->first;
			mmp[2 * it->first].insert(*t);
			it->second.erase(iit);
			it->second.erase(t);
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (a[i] != -inf)ans++;
	}
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
	{
		if (a[i] != -inf)printf("%lld ",a[i]);
	}
}
void work()
{
	read();
	solve();
}
int main()
{
	work();
	getchar();
	getchar();
}