[栈,模拟] Codeforces Round #679 (Div. 2, based on Technocup 2021 Elimination Round 1) D. Shurikens

题目:http://codeforces.com/contest/1435/problem/D

要是当时看看这个D题就好了,血亏

简单的用栈模拟即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define V vector
#define pb push_back
int main(){
    ios::sync_with_stdio(0);
    ll n;
    cin>>n;
    ll id=0;
    stack<ll> st;
    V<ll> ans(2*n+1);

    ll f=1;
    rep(cas,1,2*n){
        char in;
        cin>>in;
        if(in=='+'){
            st.push(++id);
        }
        else{
            ll nn;
            cin>>nn;
            if(st.size()==0){
                f=0;
                continue;
            }
            ll now=st.top();st.pop();
            ans[now]=nn;
            if(now<n){
                if(ans[now]<ans[now+1])f=0;
            }
        }
    }
    if(f){
        cout<<"YES"<<endl;
        rep(i,1,n)cout<<ans[i]<<' ';
        cout<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }
}
原文地址:https://www.cnblogs.com/Railgun000/p/13880679.html