cf962D

题目链接网站:http://codeforces.com/problemset/problem/962/D

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, 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 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个数的数列,然后让你变换。 变换规则是从左到有,如果有相同的数字就把它们两个合并,然后最右边的数字*2,然后一直变换到不能变换为止,再输出这个序列。


    优先队列暴力,每次从末尾取出两个数,看看这两个数是否相同,如果相同,合并之后再放进队列中,如果不同,把队尾的那个元素放进另外一个数组中,即答案

代码如下:


#include <bits/stdc++.h>

using namespace std;

const int Maxn = 1e5 + 5e4 + 2000; 

struct node {
    long long x;
    int id ;
    friend bool operator < (node a, node b) {
        if(a.x == b.x ) return a.id  >  b.id;
        return a.x > b.x;
    }
};
priority_queue <node>q;

struct node1 {
    long  long x;
    int  id;
} G[Maxn];

bool cmp(struct node1 a,struct node1 b) {
    return a.id < b.id;
}

int main() {
    int n,cnt;
    while(~scanf("%d",&n)) {
        cnt = 0;
        node s;
        for(int i = 0; i < n; i++) {
            cin >> s.x ;
            s.id = i;
            q.push(s);
        }
        while(!q.empty()) {
            if(q.size() == 1) {
                node d = q.top();
                q.pop();
                node1 dd;
                dd.x = d.x;
                dd.id = d.id;
                G[cnt++] = dd;
            } else {
                node d = q.top();
                q.pop();
                node dd = q.top();
                q.pop();
                if(d.x == dd.x) {
                    dd.x *= 2;
                    q.push(dd);
                } else {
                    node1 d1;
                    d1.x = d.x;
                    d1.id = d.id;
                    G[cnt++] = d1;
                    q.push(dd);
                }
            }
        }
        sort(G,G +cnt,cmp);
        cout << cnt << endl;
        for(int i = 0; i < cnt; i++) {
            printf("%lld%c",G[i].x,i == cnt - 1?'\n':' ');
        }


    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值