Algorithm


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 x that occurs in the array 22 or more times. Take the first two occurrences of x 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, 2x2⋅�).

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 n (2n1500002≤�≤150000) — the number of elements in the array.

The second line contains a sequence from n elements a1,a2,,an�1,�2,…,�� (1ai1091≤��≤109) — the elements of the array.

Output

In the first line print an integer k — the number of elements in the array after all the performed operations. In the second line print k 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.

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int n, id;
vector<vector<pair<int, long long> > > all;
vector<pair<int, long long> > cur, fin, tmp;
map<int, long long> mp;

bool cmp(vector<pair<int, long long> > a, vector<pair<int, long long> > b) {
	return a[0].second < b[0].second;
}

void work(int idx) {
	cur.clear();
	tmp.clear();

	sort(all[idx].begin(), all[idx].end());

	for(int i = 0, pos; i < all[idx].size(); i += 2) {
		long long val;
		if(i + 1 >= all[idx].size())
			break;

		val = all[idx][i].second * 2;
		pos = all[idx][i + 1].first;

		all[idx][i].second = -1;
		all[idx][i + 1].second = -1;

		if(mp.count(val)) {
			all[mp[val]].push_back({pos, val});
		} else {
			cur.push_back({pos, val});
		}
	}

	if(all[idx].size() & 1) {
		fin.push_back(all[idx].back());
		all[idx].back().second = -1;
	}

	while(true) {
		if(cur.size() <= 1)
			break;

		for(int i = 0, pos; i < cur.size(); i += 2) {
			long long val;
			if(i + 1 >= cur.size())
				break;

			val = cur[i].second * 2;
			pos = cur[i + 1].first;

			if(mp.count(val)) {
				all[mp[val]].push_back({pos, val});
			} else {
				tmp.push_back({pos, val});
			}
		}

		if(cur.size() & 1) {
			fin.push_back(cur.back());
		}

		cur.swap(tmp);
		tmp.clear();
	}

	fin.insert(fin.end(), cur.begin(), cur.end());
}

int main() {
	all.resize(200001);
	id = -1;

	scanf("%d", &n);
	for(int i = 0, tmp; i < n; ++i) {
		scanf("%d", &tmp);
		if(mp.count(tmp)) {
			all[mp[tmp]].push_back({i, tmp});
		} else {
			mp[tmp] = ++id;
			all[id].push_back({i, tmp});
		}
	}
	++id;

	sort(all.begin(), all.begin() + id, cmp);

	for(int i = 0; i < id; ++i)
		mp[all[i][0].second] = i;

	for(int i = 0; i < id; ++i)
		work(i);

	for(int i = 0; i < id; ++i)
		for(int j = 0; j < all[i].size(); ++j)
			if(all[i][j].second != -1)
				fin.push_back(all[i][j]);

	sort(fin.begin(), fin.end());

	printf("%d\n", int(fin.size()));
	for(int i = 0; i < fin.size(); ++i)
		printf("%lld ", fin[i].second);
	puts("");

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7
3 4 1 2 2 1 1

Output

x
+
cmd
4
3 8 2 1
Advertisements

Demonstration


Codeforces Solution-D. Merge Equals-Solution in C, C++, Java, Python,Merge Equals

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+