Stay Hungry,Stay Foolish!

C - Merge the balls

C - Merge the balls

https://atcoder.jp/contests/abc351/tasks/abc351_c

 

思路

使用stack 记录 序列路径

对栈顶两个元素 尝试做缩减处理。

 

Code

https://atcoder.jp/contests/abc351/submissions/52873456

int n;
stack<long long> sq;

int main()
{
    cin >> n;
    
    for(int i=0; i<n; i++){
        long long temp;
        cin >> temp;
        
        if (sq.size() == 0){
            sq.push(temp);
        } else {
            sq.push(temp);
            
            while(sq.size() >= 2){
                int topfirst = sq.top();
                sq.pop();
                
                int topsecond = sq.top();
                sq.pop();
                
                if (topfirst == topsecond){
                    sq.push(topfirst+1);
                } else {
                    sq.push(topsecond);
                    sq.push(topfirst);
                    break;
                }
            }
        }
    }
    
    cout << sq.size() << endl;

    return 0;
}

 

posted @ 2024-04-28 21:50  lightsong  阅读(19)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel