Ultraviolet-501 black box (priority queue or vector)

Source: Internet
Author: User

Description


Black Box

Our black box represents a primitive database. It can save an integer array and has a specialIVariable. At the initial moment black box is empty andIEquals 0. This black box processes a sequence of commands (transactions). There are two types of transactions:

  • Add (X): Put ElementXInto black box;
  • Get: IncreaseIBy 1 and giveI-Minimum Out Of All integers containing in the black box.

Keep in mind thatI-Minimum is a number locatedI-Th place after Black Box elements sorting by non-descending.

Example let us examine a possible sequence of 11 transactions:


N Transaction I Black box contents after transaction Answer
      (Elements are arranged by non-descending)  
1 Add (3) 0 3  
2 Get 1 3 3
3 Add (1) 1 1, 3  
4 Get 2 1,3 3
5 Add (-4) 2 -4, 1, 3  
6 Add (2) 2 -4, 1, 2, 3  
7 Add (8) 2 -4, 1, 2, 3, 8  
8 Add (-1000) 2 -1000,-4, 1, 2, 3, 8  
9 Get 3 -1000,-4,1, 2, 3, 8 1
10 Get 4 -1000,-4, 1,2, 3, 8 2
11 Add (2) 4 -1000,-4, 1, 2, 2, 3, 8  


It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and get transactions: 30000 of each type.


Let us describe the sequence of transactions by two integer Arrays:

1.
: A sequence of elements which are being included into black box. A values are integers not exceeding 2 000 000 000 by their absolute value,. For the example we have A= (3, 1,-4, 2, 8,-1000, 2 ).
2.
: A sequence setting a number of elements which are being encoded into black box at the moment of first, second,... and N-Transaction get. For the example we have U= (1, 2, 6, 6 ).

The Black Box algorithm supposes that natural number sequence is sorted in non-descending order, and for eachP() An inequality is valid. It follows from the fact that forP-Element of ourUSequence we perform a get transaction givingP-Minimum number from our sequence.

Input the first line of the input is an integer k, then a blank line followed by K datasets. There is a blank line between datasets.

Input for each dataset contains (in given order):. All numbers are divided by spaces and (or) carriage return characters.

Output for each dataset, write to the output black box answers sequence for a given sequence of transactions. the numbers can be separated with spaces and end-of-line characters. print a blank line between datasets. sample Input
17 43 1 -4 2 8 -1000 21 2 6 6
Sample output
3312

Question: There are n numbers and M queries. For the I-th query, the I-th query is used to find the I-th largest number in the former TMP.

Method 1: priority queue, two queues, one from small to large Q1, and the other from large to small Q2, requiring I-level large, in fact, it is equivalent to dropping the number of I after sorting, so putting the intercepted queue upside down is the queue of the largest heap, and then adjust the maximum heap each time, make sure that the first line of the team is the largest I. This can be ensured that the first line is the largest I.

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <queue>using namespace std;const int MAXN = 30010;priority_queue<int, vector<int>, greater<int> > tmp;priority_queue<int, vector<int>, less<int> > ans;int n, m;int num[MAXN];int main() {int t;scanf("%d", &t);while (t--) {while (!tmp.empty())tmp.pop();while (!ans.empty())ans.pop();scanf("%d%d", &n, &m);for (int i = 0; i < n; i++)scanf("%d", &num[i]);int cur = 0;for (int i = 0; i < m; i++) {int cnt;scanf("%d", &cnt);for (; cur < cnt; cur++) tmp.push(num[cur]);ans.push(tmp.top());tmp.pop();while (!tmp.empty() && tmp.top() < ans.top()) {cnt = tmp.top();tmp.pop();tmp.push(ans.top());ans.pop();ans.push(cnt);}printf("%d\n", ans.top());}if (t)printf("\n");}return 0;}

Method 2: divide the vector into two parts, insert one for each read

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#include <vector>using namespace std;const int MAXN = 30010;int n, m;int num[MAXN];int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++)scanf("%d", &num[i]);vector<int> v;vector<int>::iterator it;v.clear();int tmp, cur = 0;int index = 0;for (int i = 0; i < m; i++) {scanf("%d", &tmp);while (v.size() != tmp) {it = lower_bound(v.begin(), v.end(), num[cur]);v.insert(it, num[cur++]);}printf("%d\n", v[index++]);}if (t)printf("\n");}return 0;}






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.