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. 一些实用的浏览器meta

    标签: 兼容性 meta 通用 <!--声明文档使用的字符编码--> <meta charset='utf-8′> <!--viewport定义--> <me ...

  2. vue2 数据交互 vue-resource

    1.安装vue-resource到项目中,找到当前项目 输入:npm install vue-resource --save 2.安装完毕后,在main.js中导入,如下所示: import  Vue ...

  3. <Android 基础(六)> ActionBar

    介绍 Action Bar是一种新増的导航栏功能,在Android 3.0之后加入到系统的API当中,它标识了用户当前操作界面的位置,并提供了额外的用户动作.界面导航等功能.使用ActionBar的好 ...

  4. python模块详解 hashlib

    hashlib模块 用于加密相关的操作,在python3中替代了md5和sha模块,主要提供SHA和MD5算法. MD5 import hashlib m = hashlib.md5() #调用md5 ...

  5. 查看Linux网卡地址,网络地址

    查看网络地址 ip a 或ip addr show 或ifconfig,此指令在部分linux系统中不支持

  6. UE4的蓝图都能做什么

    创建关卡脚本 蓝图具有和虚幻3中Kismet一样的功能,每个关卡都由自己的蓝图,他可以: 引用和操控actors 控制使用Matinee的过场 管理关卡流,存档点以及其他关卡相关的系统 和关卡中的类蓝 ...

  7. BSP和JSP里的UI元素ID生成逻辑

    CRM WebClient UI WebClient UI渲染出来的DOM元素的这些C#_W#格式的id是在哪行ABAP代码被生成出来的? 参考我的博客WebClient UI element ID ...

  8. 每天备份NAS上的www目录到一块单独的硬盘上

    #!/bin/bash DATE=`date -d "now" +%Y%m%d` dataBackupDir=/media/2a76a963-92b1-4f74-a2c8-b7dc ...

  9. Linux MySQL 5.1源码安装

    安装必备的软件 yum install ncurses-devel -y yum install pcre pcre-devel -y yum install gcc* -y   解压缩 tar -z ...

  10. 【LOJ116】有源汇有上下界最大流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你先判断是否存在可行流.若存在,则输出最大流. 无源汇上下界可行流 在做此题之前,最好先去看看这道题目:[LOJ115]无源汇有上下界可行流. 大致思 ...