• Codeforces 926E


    You are given a sequence of positive integers a 1, a 2, …, a n.

    While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a single integer x + 1 on their place. This way the number of elements in the sequence is decreased by 1 on each step.

    You stop performing the operation when there is no pair of equal consecutive elements.

    For example, if the initial sequence is [5, 2, 1, 1, 2, 2], then after the first operation you get [5, 2, 2, 2, 2], after the second — [5, 3, 2, 2], after the third — [5, 3, 3], and finally after the fourth you get [5, 4]. After that there are no equal consecutive elements left in the sequence, so you stop the process.

    Determine the final sequence after you stop performing the operation.

    Input

    The first line contains a single integer n (2 ≤ n ≤ 2·105) — the number of elements in the sequence.

    The second line contains the sequence of integers a 1, a 2, …, a n (1 ≤ a i ≤ 109).

    Output

    In the first line print a single integer k — the number of elements in the sequence after you stop performing the operation.

    In the second line print k integers — the sequence after you stop performing the operation.

    Examples
    Input
    6
    5 2 1 1 2 2
    Output
    2
    5 4
    Input
    4
    1000000000 1000000000 1000000000 1000000000
    Output
    1
    1000000002
    Input
    7
    4 10 22 11 12 5 6
    Output
    7
    4 10 22 11 12 5 6
    Note
    The first example is described in the statements.

    In the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third operation the sequence is [1000000002].

    In the third example there are no two equal consecutive elements initially, so the sequence does not change.

    题目大意:

    输入n,接下来输入一个n个数的数组,你需要对其从左到右进行合并,如果有两个相邻的数相等,就将其合并,该数变为num + 1,要求输出全部合并完后数组内的个数,第二行按顺序输出合并后的数组元素。

    解题思路:

    从左到右合并,用栈模拟即可。输入一个数,判断该数和栈顶是否相等,如果相等则栈顶出栈,num ++ ,继续判断,直到栈空或num和栈顶元素不相等,这时将num入栈,全部输入完后则模拟也结束了,因为栈中出栈顺序是反的,则可以用vector存出栈的元素,然后逆序输出即可。

    Code:

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    const int N = 2e5 + 50;
    const int inf = 0x3f3f3f3f;
    stack<ll > st;
    int main()
    {
    	ios::sync_with_stdio(false);
    	int n;
    	cin >> n;
    	for (int i = 1; i <= n; i ++)
    	{
    		ll s;
    		cin >> s;
    		while (!st.empty() && s == st.top())//如果满足条件则s不断++,不断出栈再判断
    		{
    			st.pop();
    			s++;
    		}
    		st.push(s);
    	}
    	cout << st.size() << endl;
    	vector<ll > v;
    	while (!st.empty())//出栈顺序是逆序的,需要借助vector调整
    	{
    		v.push_back(st.top());
    		st.pop();
    	}
    	reverse(v.begin(), v.end());
    	for (auto i : v)
    		cout << i << ' ';
    	cout << endl;
    	return 0;
    }
    
  • 相关阅读:
    【转】读《冰鉴》有感:职场生存术——企业观人十一招
    [转]msn主要端口问题
    关于linux下的openmp编程基础[转]
    C#运用正则表达式智能获取html模版页中模版信息的应用
    获取当前程序文件的路径
    ASP对UTF8编码支持有问题
    论.NET反射、委托技术与设计模式关系
    序列化与反序列化
    利用反射将数据读入实体类
    随心所欲操作Enum枚举类型
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294172.html
Copyright © 2020-2023  润新知