UVA - 501 Black Box (优先队列或vector)

Description

 Black Box 

Our Black Box represents a primitive database. It can save an integer array and has a special
i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions:

  • ADD(x): put element x into Black Box;
  • GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box.

Keep in mind that i-minimum is a number located at i-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 included 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 each
p ( ) an inequality
is valid. It follows from the fact that for the
p-element of our u sequence we perform a GET transaction giving
p-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

1

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample Output

3
3
1
2

题意:有n个数,m次询问,对于第i次询问是:在前tmp个中找第i大的数

方法一:优先队列,两个队列,一个从小到大q1,一个从大到小的q2,要求第i大的,其实就是相当于排序后截掉第i个数之后的,那么将这个截掉后的队列倒置就是个最大堆的队列,然后每次调整这个最大堆就行了,保证队首是第i大就是了,这个每次都加进去一个就可以保证是第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;
}

方法二:vector的二分,每读一个就插入一个

#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;
}

UVA - 501 Black Box (优先队列或vector)

时间: 2024-11-05 23:36:07

UVA - 501 Black Box (优先队列或vector)的相关文章

uva 501 - Black Box(优先队列)

题目链接:uva 501 - Black Box 题目大意:有一个集合,给定元素进入集合的顺序,现在有Q次查询,给定每次查询在第几个元素进入集合后,对于每i次查询,输出集合中第i小的数. 解题思路:用两个优先队列维护,队列a优先出值大的,队列b优先出值小的,在第i次询问前,保证a队列中有i-1个元素元素,并且抱枕都比b中的小,然后每次询问输出b队列的首元素,并且将它放到a队列中. #include <cstdio> #include <cstring> #include <q

uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母.首先输入m条边,当经过小写字母时需要付一单位的过路费,当经过大写字母时,要付当前财务的1/20做过路费.问在起点最少需要带多少物品使到达终点时还有k个物品.当有多条符合条件的路径时输出字典序最小的一个. 思路:已知终点的权值,那么可以从终点向前推.求终点到起点的最短路径,然后按字典序打印路径. 比较难处理的是:向前推时前驱节点的权值计算.列个方程算算就可以了,主要时不能整除的情况. 计算前驱结点dis值的时候,

POJ 1442 Black Box(优先队列)

题目地址:POJ 1442 这题是用了两个优先队列,其中一个是较大优先,另一个是较小优先.让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行判断,如果这个数比较大优先的队列的队头要小,就让它加入这个队列,队列头移到较小优先的队列中.然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中. 代码如下: #include <iostream> #include <cstdio> #include <string>

UVa 136 Ugly Numbers(优先队列)

题意  质因数只可能有2,3,5的数称为丑数  输出第1500个丑数 STL优队列应用  1是丑数 丑数的2,3,5倍都是丑数  用优先队列模拟就行了 #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; typedef long long ll; //priority_queue<ll, vector<ll>, g

POJ 1442-Black Box(优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7436   Accepted: 3050 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

UVA 1412 - Fund Management(用vector容器模拟状态的状压dp)

Frank is a portfolio manager of a closed-end fund for Advanced Commercial Markets (ACM ). Fund collects money (cash) from individual investors for a certain period of time and invests cash into various securities in accordance with fund's investment

uva 230 Borrowers(摘)&lt;vector&gt;&quot;结构体“ 膜拜!

I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves, and creators of odd volumes. --Charles Lamb, Essays of Elia (1823) 'The Two Races of Men' Like Mr. Lamb, librarians have their problems with borrowe

uva 10474 Where is the Marble?[ vector ]

思路:sort+low_bound()二分 #include<vector> #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<set> using namespace std; int main() { vector<int>v; int n,m; int cas=1; while(scanf("

UVA 11997--K Smallest Sums+优先队列用于多路归并

题目链接:点击进入 先考虑两个数组A,B的情况,这样总共有n^2种情况;将A,B数组排序后,我们可以将所有情况组织成n张表: 表1: A[1]+B[1]<=A[1]+B[2]<=--<=A[1]+B[n]. 表2: A[2]+B[1]<=A[2]+B[2]<=--.<=A[2]+B[n]. --. 表n: A[n]+B[1]<=A[n]+B[2]<=--..<=A[n]+B[n] 这n张表都是有序的,所以可以以多路归并的思想求出其中前n个最小的元素.对