ACM刷题之codeforces————Merge Equals

版权声明:本文为小时的枫原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaofeng187/article/details/79912959
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, 2x2⋅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 (2n1500002≤n≤150000) — the number of elements in the array.

The second line contains a sequence from nn elements a1,a2,,ana1,a2,…,an (1ai1091≤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 kkintegers — 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.


题意:给你一组数,找到其中最小的数(最少有2个或以上),将最左边的数删除,左数第二的*2。然后重复操作

例如 [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].


做法:这里用优先队列来做。

以数值小为优先,打上下标后加入优先队列,然后取出两个比较,若相同,则去掉一个,将另一个*2 放回队列。若不同,将小的去掉,大的压回队列。重复直至队列为空或只有一个。 

然后sort结构体排序输出就好了。

下面是ac代码:

#include<bits/stdc++.h>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=2e5+7;

struct node{
	__int64 val,sub;
	
	bool operator < (const node & a) const {
		if (val == a.val) {
			return a.sub < sub;
		}
		return a.val < val;
	}
};



priority_queue<node> qu;

struct ans{
	__int64 v, s;
};
ans a[N];
bool cmp(ans a, ans b) {
	return a.s < b.s;
}

int main()
{
	//freopen("f:/input.txt", "r", stdin);
	int n, i;
	scanf("%d", &n);
	node nd,md;
	for (i = 0 ; i < n ; i++) {
		scanf("%I64d", &nd.val);
		nd.sub = i;
		qu.push(nd);
	}
	int cnt = 0;
	while (!qu.empty()) {
		
		if (qu.size() == 1) {
			nd = qu.top(); qu.pop();
			
			a[cnt].v = nd.val;
			a[cnt].s = nd.sub;
			++cnt;
			break;
		}
		// 取出顶部两个 
		nd = qu.top(); qu.pop();
		md = qu.top(); qu.pop();
		
		if (nd.val == md.val) {
			
			md.val += nd.val;
			md.sub = max(nd.sub, md.sub);
			qu.push(md);
			
		} else {
			a[cnt].v = nd.val;
			a[cnt].s = nd.sub;
			++cnt;
			qu.push(md);
			
		}
	}
	
	sort(a, a + cnt, cmp);
	printf("%d\n%I64d", cnt, a[0].v);
	for (i = 1 ; i < cnt; i ++) {
		printf(" %I64d", a[i].v);
	} 
	printf("\n");
}

猜你喜欢

转载自blog.csdn.net/xiaofeng187/article/details/79912959