Uva501-black box (vector + lower_bound)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.