Uva501-black box (vector + lower_bound)
Question Link
Here are two operations: Add num and add a number to the Black Box. The original black box is empty. And x = 0.get
It is to increase X from 1 and then take out the small number of X. The U sequence in the question indicates that the get operation is performed after the UI number is added to the Black Box.
Solution: Use Vector to store the data so that we can randomly retrieve the small I number. Then use the lower_bound function to find the position where the number is greater than or equal to the first number. insert this number into this position, and the complexity is n? Log (N) and insert it to the corresponding position of the vector.
Code:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 3e4 + 5;vector<ll> v;vector<ll>::iterator it;int n, m;ll num[maxn];int op;int main () { int T; scanf ("%d", &T); while (T--) { scanf ("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf ("%lld", &num[i]); v.clear(); int dex = 0; for (int i = 0; i < m; i++) { scanf ("%d", &op); while (dex < op) { it = lower_bound(v.begin(), v.end(), num[dex]); if (it != v.end()) v.insert(it, num[dex++]); else v.push_back(num[dex++]); } printf ("%lld\n", v[i]); } if (T) printf ("\n"); } return 0;}
Uva501-black box (vector + lower_bound)