AtCoder Beginner Contest 351 C题

C - Merge the balls

Time Limit: 2 sec / Memory Limit: 1024 MB

Score 250 points

Problem Statement

You have an empty sequence and 𝑁N balls. The size of the 𝑖i-th ball (1≤𝑖≤𝑁) is ​2^A[i]

You will perform N operations.
In the 𝑖i-th operation, you add the 𝑖i-th ball to the right end of the sequence, and repeat the following steps:

  1. If the sequence has one or fewer balls, end the operation.
  2. If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation.
  3. If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process.

Determine the number of balls remaining in the sequence after the N operations.

Constraints

  • 1≤𝑁≤2×105
  • 0≤Ai​≤109
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

𝑁
A1​ 𝐴2​ …… 𝐴𝑁

Output

Print the number of balls in the sequence after the 𝑁 operations.

 

Sample Input 1Copy

Copy

7
2 1 1 3 5 3 3

Sample Output 1Copy

Copy

3

Sample Input 2Copy

Copy

5
0 0 0 1 2

Sample Output 2Copy

Copy

4

思路:

这道题利用动态数组vector来模拟题目的要求,其中需要注意的是vector数下标是从0 开始的,因此判断最右面的元素和倒数第二个右面元素的时候要注意其下标,再就是注意它放入的数据是2 的A[i]的次幂。其他只要掌握vector数组的使用方法,会vector.pop_back,应该就没问题了。

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

typedef long long ll;

void solve()
{
    int n; cin>>n;
    vector<int> vec;
    while(n--)
    {
        int x; cin>>x;
        vec.push_back(x);
        while(vec.size()>=2 && (vec[vec.size()-2]==vec[vec.size()-1]) )
        {
            int x=vec[vec.size()-1];
            vec.pop_back();
            vec.pop_back();
            vec.push_back(x+1);
        }
    }
    cout<<vec.size();
}

int main()
{
    int t=1; //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

相关推荐

  1. IOS面试object-c 31-40

    2024-05-04 20:50:05       43 阅读
  2. LeetCode(66,69,35,88)--《c++》

    2024-05-04 20:50:05       27 阅读
  3. 安卓UI面试 31-35

    2024-05-04 20:50:05       39 阅读
  4. IOS面试编程机制 31-35

    2024-05-04 20:50:05       35 阅读
  5. AT_abc351_c [ABC351C] Merge the balls 题解

    2024-05-04 20:50:05       36 阅读
  6. Redis面试35

    2024-05-04 20:50:05       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 20:50:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 20:50:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 20:50:05       82 阅读
  4. Python语言-面向对象

    2024-05-04 20:50:05       91 阅读

热门阅读

  1. 车载开发-Android Automotive平台

    2024-05-04 20:50:05       34 阅读
  2. git解决冲突问题

    2024-05-04 20:50:05       34 阅读
  3. 修改ETCD返回数据限额

    2024-05-04 20:50:05       32 阅读
  4. 2024/5/3 C++五一

    2024-05-04 20:50:05       38 阅读
  5. PPT基础

    PPT基础

    2024-05-04 20:50:05      35 阅读
  6. SpringCloud相关面试题(详细解答)

    2024-05-04 20:50:05       33 阅读
  7. 2024十大免费cms建站系统有哪些

    2024-05-04 20:50:05       35 阅读
  8. 某夸克pan之搜索接口

    2024-05-04 20:50:05       33 阅读
  9. AI做画的算法原理

    2024-05-04 20:50:05       24 阅读
  10. 数字化思维的目的与价值,你真的懂吗?

    2024-05-04 20:50:05       28 阅读