竟然卡输入??!!有意思么~用堆做不好玩,换了个multiset玩玩,最好还是优先队列哦~
PS,这种能用stl解决的数据结构题其实没大有意思。。
#include <iostream> #include <set> #include <stdio.h> using namespace std; #define MAXN 30010 int num[MAXN]; multiset<int> a; multiset<int> b; int main() { int n, q; cin >> n >> q; for (int i = 1; i <= n; ++i) scanf("%d", &num[i]); int start = 1; for (int i = 1; i <= q; ++i) { int t; cin >> t; while (t >= start) { if (!b.empty()) { multiset<int>::iterator b_it = b.end(); --b_it; if (num[start]<*b_it) { int temp = *b_it; b.erase(b_it); b.insert(num[start]); a.insert(temp); } else a.insert(num[start]); } else { a.insert(num[start]); } ++start; } multiset<int>::iterator a_it = a.begin(); printf("%d\n", *a_it); int temp = *a_it; a.erase(a_it); b.insert(temp); } } |