Codeforces 821C

821C - Okabe and Boxes

思路:模拟。因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中)。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>st; 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,temp=1,ans=0;
    string s;
    cin>>n;
    for(int i=0;i<n*2;i++)
    {
        cin>>s;
        if(s[0]=='a')
        {
            int a;
            cin>>a;
            st.push_back(a);
        }
        else
        {
            if(!st.empty()&&st.back()!=temp)st.clear(),ans++;
            else if(!st.empty())st.pop_back();
            temp++;
        }
    }
    cout<<ans<<endl; 
    return 0;
}
原文地址:https://www.cnblogs.com/widsom/p/7250223.html