0
点赞
收藏
分享

微信扫一扫

AtCoder Beginner Contest 240 D

杨小羊_ba17 2022-02-24 阅读 110
c++

先看题面
Problem Statement
Takahashi has N balls. Each ball has an integer not less than 2 written on it. He will insert them in a cylinder one by one. The integer written on the i-th ball is ai.
The balls are made of special material. When k balls with k (k≥2) written on them line up in a row, all these k balls will disappear.

For each i (1≤i≤N), find the number of balls after inserting the i-th ball.

Constraints
1≤N≤2×10^5

2≤ai ≤2×10^5 (1≤i≤N)
All values in input are integers.
大意就是高桥有N个球。每个球上都写有一个不小于2的整数。他将它们一个接一个地插入一个圆柱体中。第i个球上的整数是ai

这些球是由特殊材料制成的。当k (k≥2)个球排成一行时,所有这些k个球将消失。

对于每个i(1≤i≤N),求插入第i个球后的球数。
很明显的pair。
可以用一个STL里的stack来存num(只能访问 stack 顶部的元素;只有在移除 stack 顶部的元素后,才能访问下方的元素)
stack要是pair类型的再不断的push和pop,每一次记一次cnt
代码

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
stack<pii> num;
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int n, cnt = 0, x;
    cin >> n;
    num.push({0, 0});
    for(int i = 0; i < n; i++){
        cin >> x;
        cnt++;
        pii u = num.top();
        if(u.f == x){
            if(u.s + 1 == x){
                cnt -= x;
                num.pop();
            }
            else{
                num.pop();
                num.push({u.f, u.s + 1});
            }
        }
        else num.push({x, 1});
        cout << cnt << '\n';
    }
}

拜拜,给个赞再走呗

举报

相关推荐

0 条评论