题解:
用优先队列和pair来模拟栈,这个很好想。关键点是开第三个栈,因为可能有多次A栈和B栈的合并,如果转移次数多了会超时。这时候就用到第三个栈了,把所以合并都合并到C栈里去。取的时候,先从A/B栈里取。如果是空栈,就从C栈里取;
- 思维题+优先队列+栈
Accepted 5818 858MS 2112K 1183 B G++ #include "cstdio" #include "queue" using namespace std; typedef pair<int, int> PII; priority_queue<PII> a, b, c; char op[10], ch; int main() { int n, k, ca = 1; while (scanf("%d", &n) && n) { printf("Case #%d: ", ca++); for (int i = 1; i <= n; i++) { scanf("%s", op); if (op[1] == 'u') { scanf(" %c%d", &ch, &k); if (ch == 'A') { a.push(make_pair(i, k)); } else { b.push(make_pair(i, k)); } } else if (op[1] == 'o') { scanf(" %c", &ch); if (ch == 'A') { if (a.empty()) { printf("%d ", c.top().second); c.pop(); } else { printf("%d ", a.top().second); a.pop(); } } else { if (b.empty()) { printf("%d ", c.top().second); c.pop(); } else { printf("%d ", b.top().second); b.pop(); } } } else { gets(op); while (!a.empty()) { c.push(a.top()); a.pop(); } while (!b.empty()) { c.push(b.top()); b.pop(); } } } while (!a.empty()) { a.pop(); } while (!b.empty()) { b.pop(); } while (!c.empty()) { c.pop(); } } return 0; }