目录
1-1 特殊堆栈
#include <iostream> #include <algorithm> #include <vector> #include <stack> #include <string> using namespace std; stack<int> box; vector<int> up; //维护非递减序列 int main() { int n; cin >> n; while(n--) { string s; cin >> s; if(s[1] == 'u') { int key; cin >> key; box.push(key); auto pos = upper_bound(up.begin(), up.end(), key); up.insert(pos, key); } else if(s[1] == 'o') { if(box.empty()) puts("Invalid"); else { int key = box.top(); box.pop(); auto pos = lower_bound(up.begin(), up.end(), key); up.erase(pos); cout << key << endl; } } else { if(box.empty()) puts("Invalid"); else { cout << up[(up.size() - 1) / 2] << endl; } } } return 0; }
1-2 Windows消息队列
维护优先级
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <string> using namespace std; priority_queue<int, vector<int>, greater<int>> q; //维护优先级的值 map<int, string> m; int main() { int n; cin >> n; while(n--) { string s; cin >> s; if(s[0] == 'P') { int key; cin >> s >> key; m[key] = s; q.push(key); } else { if(q.empty()) puts("EMPTY QUEUE!"); else { int key = q.top(); cout << m[key] << endl; q.pop(); } } } return 0; }
1-3 银行业务队列简单模拟
#include <bits/stdc++.h> using namespace std; queue<int> A, B; vector<int> C; int main() { int n; cin >> n; while(n--) { int key; cin >> key; if(key % 2 == 0) B.push(key); else A.push(key); } for(int i = 1; A.size() && B.size(); i++) //用i模拟单位时间 { if(A.size()) { C.push_back(A.front()); A.pop(); } if(i % 2 == 0 && B.size()) { C.push_back(B.front()); B.pop(); } } while(A.size()) { C.push_back(A.front()); A.pop(); } while(B.size()) { C.push_back(B.front()); B.pop(); } for(int i = 0; i < C.size(); i++) { cout << C[i]; if(i != C.size() - 1) cout << ' '; } return 0; }
1-4 链表去重
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; int head, e[N], ne[N]; //模拟单链表 bool st[N]; vector<int> a, b, c; int main() { cin >> head >> n; memset(ne, -1, sizeof ne); for(int i = 0; i < n; i++) { int add, key, next; cin >> add >> key >> next; e[add] = key; ne[add] = next; } for(int i = head; i != -1; i = ne[i]) c.push_back(i); //依次存结点地址 for(int i = 0; i < c.size(); i++) { int j = abs(e[c[i]]); //获取键值的绝对值 if(!st[j]) { st[j] = true; a.push_back(c[i]); } else { b.push_back(c[i]); } } printf("%05d %d ", a[0], e[a[0]]); for(int i = 1; i < a.size(); i++) { printf("%05d\n", a[i]); printf("%05d %d ", a[i], e[a[i]]); } printf("-1\n"); if(b.size()) //注意测试点2:无需去重 { printf("%05d %d ", b[0], e[b[0]]); for(int i = 1; i < b.size(); i++) { printf("%05d\n", b[i]); printf("%05d %d ", b[i], e[b[i]]); } printf("-1\n"); } return 0; }
1-5 出栈序列的合法性
#include <bits/stdc++.h> using namespace std; int a[1010], idx; int main() { int m, n, k; cin >> m >> n >> k; while(k--) { for(int i = 0; i < n; i++) cin >> a[i]; idx = 1; stack<int> box; for(int i = 0; i < n; i++) { int x = a[i]; while((box.empty() || box.top() != x) && box.size() < m && idx <= n) box.push(idx++); if(box.top() == x) box.pop(); else { puts("NO"); break; } if(i == n - 1) puts("YES"); } } return 0; }