1 #include<iostream>
2 #include<cmath>
3 #include<cstring>
4 #include<string>
5 #include<vector>
6 #include<stack>
7 #include<set>
8 using namespace std;
9
10 typedef pair<int, int> pii;
11 int main(){
12 int n; cin >> n;
13 vector<pii> v(2 * n + 10);
14 vector<int> arr(n + 1);
15 stack<int> iter;
16
17 int cnt = 0;
18 bool flag = false;
19 for(int i = 1 ; i <= 2 * n ; i++){
20 int a; char type;
21 cin >> type;
22 if(type == ‘+‘){
23 cnt++;
24 iter.push(cnt);//
25 v[i] = {0, 0};
26 }else{
27 cin >> a;
28 v[i] = {1, a};
29 if(flag) continue;
30 if(iter.empty()){
31 flag = 1;
32 }else{
33 arr[iter.top()] = a;
34 iter.pop();
35 }
36 }
37 }
38
39 if(flag){
40 printf("NO\n");
41 return 0;
42 }
43
44 int tot = 1;
45 set<int> st;
46 for(int i = 1 ; i <= 2 * n ; i++){
47 if(!v[i].first){//原来是加号
48 st.insert(arr[tot]);
49 tot++;
50 }else{
51 int now = *st.begin();
52 st.erase(st.begin());
53 if(now != v[i].second){
54 cout << "NO" << endl;
55 return 0;
56 }
57 }
58 }
59
60 cout << "YES" << endl;
61 for(int i = 1 ; i < n ; i++){
62 cout << arr[i] << " ";
63 }
64 cout << arr[n] << endl;
65
66 return 0;
67 }
原文:https://www.cnblogs.com/ecustlegendn324/p/14071274.html