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. Servlet细节整合

    最近在复习Servlet,发现其中有很多细节方面的东西都没有接触到,只是学了大概 1.请求转发和请求重定向的区别 2.输入参数为中文时候的乱码问题 3.Web工程中的目录写法 下面分别阐述 1.请求转 ...

  2. npm下载某个版本

    如果我想要引入的是Jquery的1.7.2版本,则输入npm intall jquery@1.7.2,那么npm包管理器就会帮助你下载jquery1.7.2的版本到你当前操作目录下的node_modu ...

  3. Android Studio 小技巧(1):如何导入AAR文件

    1. 导入AAR.JAR文件 File- > New -> New Module 这样子AAR文件就导入了,然后在app中的build.gradle中做如下添加 dependencies ...

  4. Android Studio 创建虚拟机失败 Failed to load 解决办法

    Name: Nexus_S_API_21 CPU/ABI: ARM (armeabi) Path: H:\Users\Pavkoo\.android\avd\Nexus_S_API_21.avd Er ...

  5. Android实现异步的几种方法

    在Android项目中,有经验的开发人员都知道,一些耗时的IO操作等都必须在子线程中去操作,那么可以有哪些方法来开启子线程呢,一般可以使用Java中自带的几种方法,也可以使用Andorid特有的一些类 ...

  6. form中的action与<url-pattern>的理解

    一.<form action="Test/Login" method="post"> 在action中有两种表示方式: 1."/Test/ ...

  7. org.springframework.beans.factory.BeanNotOfRequiredTypeException

    写一个代码:关于Spring Bean的装配.基于annotation实现的范例代码. 出现了错误: 十一月 14, 2018 4:51:01 下午 org.springframework.conte ...

  8. centos6.5_64bit-kvm安装部署

    kvm部署安装   目录 kvm部署安装... 1 一.kvm部署... 1 1.关闭selinux和防火墙... 1 2.查看主机是否支持虚拟化... 1 3.安装kvm和其他虚拟化软件包... 1 ...

  9. (原创)北美信用卡(Credit Card)个人使用心得与总结(个人理财版) [精华]

    http://forum.chasedream.com/thread-766972-1-1.html 本人2010年 8月F1 二度来美,现在credit score 在724-728之间浮动,最高的 ...

  10. vue - 绑定css、style

    1.绑定html css1.1对象语法: 传给 v-bind:class 一个对象,以动态地切换 class <div v-bind:class="{ active: isActive ...