• Educational Codeforces Round 42 (Rated for Div. 2) D.Merge Equals (优先队列)


    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 x
    that occurs in the array 2 or more times. Take the first two occurrences of x 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⋅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]
    . 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]

    .

    If the given array is look like [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]

    .
    Input

    The first line contains a single integer n
    (2≤n≤150000

    ) — the number of elements in the array.

    The second line contains a sequence from n
    elements a1,a2,…,an (1≤ai≤109

    ) — the elements of the array.
    Output

    In the first line print an integer k
    — the number of elements in the array after all the performed operations. In the second line print k

    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个数的数组,现在需要不断的进行如下操作,直到无法操作为止,每次操作将数组中两个相同的最小的数删去,再将它们的和放在刚才删去的靠右的数的位置上。将最后得到的数组,输出它的长度,以及按顺序输出数组中的元素。

    分析:据题意,需要维护数组中的最小值,可以用优先队列来维护。

               首先将每个数编上号,作为它的顺序编号,方便最后的输出。然后将它们放入到优先队列中。

               取出当前的最小值,再判断与队头的值是否相等,相等则取出队头,再将它们的和编号后放入

               不相等的话,那么后面也不会对这个数进行操作,将它直接放入表示答案的容器中,进行下一次优先队列的判断。

               当优先队列为空时,将表示答案的容器按照编号排序,再输出即可。

    代码如下:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <map>
    #include <vector>
    #include <cmath>
    #include <queue>
    using namespace std;
    typedef long long LL;
    #define INF 0x3f3f3f3f
    const int MAXN=2e5+100;
    LL n;
    LL sz;
    struct node
    {
      LL x;
      LL id;
    }a[MAXN];
    
    bool operator <(node a,node b)
    {
      if(a.x!=b.x)
      return a.x>b.x;
      return a.id>b.id;
    }
    
    priority_queue<node>Q;
    vector<node>ans;
    
    bool cmp(node a,node b)
    {
        return a.id<b.id;
    }
    int main()
    {
    
        ios::sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++)
        {
          cin>>a[i].x;
          a[i].id=i;
          Q.push(a[i]);
        }
    
        while(!Q.empty())
        {
          node k=Q.top();
          Q.pop();
          if(Q.size()>=1&&Q.top().x==k.x)
          {
             node next1;
             next1.x=k.x*2;
             next1.id=Q.top().id;
             Q.pop();
             Q.push(next1);
          }
          else
          ans.push_back(k);
        }
    
        sort(ans.begin(),ans.end(),cmp);
    
        cout<<ans.size()<<endl;
        for(int i=0;i<ans.size();i++)
            i==0?cout<<ans[i].x:cout<<" "<<ans[i].x;
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    Vim深入研究
    信息安全系统设计基础第十三周学习总结——20135308
    信息安全系统设计基础实验五:通讯协议设计
    信息安全系统设计基础第十二周学习总结 ——20135308
    信息安全系统设计基础实验四:外设驱动程序设计
    信息安全系统设计基础实验三:实时系统的移植
    GDB深入研究——20135308芦畅
    信息安全系统设计基础第十一周学习总结——20135308
    第九次ScrumMeeting博客
    第八次ScrumMeeting博客
  • 原文地址:https://www.cnblogs.com/a249189046/p/8821613.html
Copyright © 2020-2023  润新知