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 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.

题意:给出n个数,接下来对这个数列进行一个操作,即从左往右找最小的两个相同的数,然后将左边那个删除,右边那个数的值乘以2。不断进行以上操作,直到无相同的数为止,输出最终那个数列。

思路:

map中套一个set的操作,mp[i]指的是值为i的数字的下标合集。从小到大的遍历mp,如果有两个,则将其合并为一个放到2*i的集合中去。如果仅含一个元素,记录其的位置即可。

代码:

 #include <bits/stdc++.h>

 using namespace std;
typedef long long ll;
const int maxn = 2e6+;
ll n;
ll a[maxn];
map<ll, set<ll> >mp;
ll b[maxn];
int main()
{
scanf("%I64d",&n);
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
mp[a[i]].insert((ll)i);
}
ll ans=;
for (auto it: mp)
{
while(it.second.size()>=)
{
it.second.erase(it.second.begin());
mp[it.first*].insert(*it.second.begin());
it.second.erase(it.second.begin());
}
if(it.second.size()==)
{
int id=*it.second.begin();
b[id]=it.first;
ans++;
}
}
printf("%I64d\n",ans);
for(int i=;i<=n;i++) if(b[i]>) printf("%I64d ",b[i]);
return ;
}

Educational Codeforces Round 42D. Merge Equals(STL)的更多相关文章

  1. Educational Codeforces Round 51 D. Bicolorings(dp)

    https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...

  2. Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分

    A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...

  3. Codeforces Round #624 (Div. 3)(题解)

    Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...

  4. Codeforces Round #219 (Div. 1)(完全)

    戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...

  5. D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))

    模拟题,运用强大的stl. #include <iostream> #include <map> #include <algorithm> #include < ...

  6. Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))

    题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...

  7. (模拟)关于进制的瞎搞---You Are Given a Decimal String...(Educational Codeforces Round 70 (Rated for Div. 2))

    题目链接:https://codeforc.es/contest/1202/problem/B 题意: 给你一串数,问你插入最少多少数可以使x-y型机器(每次+x或+y的机器,机器每次只取最低位--% ...

  8. Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)

    DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把 ...

  9. Educational Codeforces Round 30D. Merge Sort

    归并排序的逆操作,每次二分时把第二段第一位与第一段最后一位开始往前第一个比它大的数交换位置 可以用归并排序验证答案对不对 #include<bits/stdc++.h> #define f ...

随机推荐

  1. 编写xml文件的几个注意事项

    作者:朱金灿 来源:http://blog.csdn.net/clever101 xml注释的规范是这样的: <!-xml注释内容 --> 值得注意的是任何xml注释都必须放在<?x ...

  2. Azure:陪伴你们,是我最长情的告白

    立即访问http://market.azure.cn

  3. Azure 进阶攻略 | 文件完整性,你打算如何证明?

    假设你是一位独立软件开发者,通过自己的网站提供软件下载.网站完全托管在 Azure 中,并且软件下载也是通过 Azure Blob 存储和 Azure CDN 服务提供的. 这做法真不错,不需要自己管 ...

  4. Java I/O 工作机制(二) —— Java 的 I/O 的交互方式分析

    简介: BIO:同步阻塞式IO,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通过线程池机制改善.  ...

  5. CMAKE 安装

    下载 解压  https://cmake.org/download/ https://cmake.org/files/v3.7/cmake-3.7.1.tar.gz yum install gcc - ...

  6. Git入门体验

    Git这个东西我也是最近才知道的,然后知道后却发现一个事实:自己真的是太LOW啦!竟然连Git都不知道!!!??? Git 在实际协同工作时会为我们提供巨大帮助, 下面简单介绍一下Git的用法: 一. ...

  7. 不同编程语言在发生stackoverflow之前支持的调用栈最大嵌套层数

    今天我的一位同事在微信群里发了一张图片,勾起了我的好奇心:不同编程语言支持的函数递归调用的最大嵌套层数是? Java 1.8 private static void recur(int i){ Sys ...

  8. Android(java)学习笔记67:Android Studio新建工程中的build.gradle、settings.gradle

    随着信息化的快速发展,IT项目变得越来越复杂,通常都是由多个子系统共同协作完成.对于这种多系统.多项目的情况,很多构建工具都已经提供了不错的支持,像maven.ant.Gradle除了借鉴了ant或者 ...

  9. 引用类型(一):Object类型

    对象表示方式 1.第一种方式:使用new操作符后跟Object构造函数 var person = new Object();<br/> person.name = 'Nicholas';& ...

  10. P1036 选数

    题目描述 已知 nn 个整数 x_1,x_2,…,x_nx1​,x2​,…,xn​,以及11个整数kk(k<nk<n).从nn个整数中任选kk个整数相加,可分别得到一系列的和.例如当n=4 ...