poj1442 Black Box 栈和优先队列

题意:有n个数,按顺序加入,求加入前Gi个数时第i个最小的数是多少

思路:这里需要用到STL里的优先队列priority_queue,建一个大堆和一个小堆,若想在一个无序的序列里找第n个小的数,可以先把一个序列的n-1个数放入大堆(即假设这n-1个数是该序列里最小的),然后向小堆里push数,若小堆头元素<大堆头元素(即最小的元素比最大的元素大,说明大堆里的数还不是最小的),则交换两个数。难点:这里如果只用一个小堆的话会超时;另外每输入的一个m数,要查询的最小的数正好是第i++个,即这里每输出一个数就把该数存入大堆里,正好使大堆里数的个数不断++(由0个不断增加),以方便下一次查询第i++个小的。

Black Box
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7205   Accepted: 2930

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 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.

Let us examine a possible sequence of 11 transactions:

Example 1

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(1), A(2), ..., A(M): 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, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).

2. u(1), u(2), ..., u(N): 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 u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M 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 A(1), A(2), ..., A(u(p)) sequence.

Input

Input contains (in given order): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers are divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

Sample Input

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

Sample Output

3
3
1
2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 #include <stack>
 9 #include <queue>
10 #include <functional>
11 #include <vector>
12 #include <map>
13 //priority_queue<int> pq;
14 //queue<int >q;
15 using namespace std;
16
17 #define M 0x0f0f0f0f
18 #define min(a,b) (a>b?b:a)
19 #define max(a,b) (a>b?a:b)
20 int main()
21 {
22     int m,n;
23     int i,j,b,c,t;
24     int a[30005];
25     scanf("%d %d",&n,&m);
26     priority_queue< int,vector<int>,greater<int> >small;
27     priority_queue< int>large;
28     for(i=0; i<n; i++)
29         scanf("%d",&a[i]);
30     c=0;
31     for(i=0; i<m; i++)
32     {
33         scanf("%d",&b);
34         while(c<b)
35         {
36             small.push(a[c]);
37             if(!large.empty()&&small.top()<large.top())
38             {
39                 t=small.top();
40                 small.pop();
41                 small.push(large.top());
42                 large.pop();
43                 large.push(t);
44             }
45             c++;
46         }
47         printf("%d\n",small.top());
48         //精华!!!!
49         large.push(small.top());//每次i++,让large里放一个数,使得以后每次比较时
50         small.pop();           //保证small里的第一个数是第i个最小的!!!!!
51     }
52     return 0;
53 }

 

poj1442 Black Box 栈和优先队列

时间: 2024-10-12 09:04:28

poj1442 Black Box 栈和优先队列的相关文章

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 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

单调队列、单调栈、优先队列模板

目录 单调栈.单调队列及优先队列 1.单调队列 2.单调栈 3.优先队列 单调栈.单调队列及优先队列 1.单调队列 单调队列的描述:指队列中元素之间关系具有单调性,而且队首和队尾都可以出队,但是只有队尾可以进行入队操作.其重要作用是找到前n个后者后n个数的最值. 其具体操作是:假设单调队列是单调递减队列,假设在插入元素v时,将队列尾部的元素同v比较,如果队列尾部的元素不大于元素v,我们直接删除队尾元素,再将队尾元素与v比较,直至队尾元素比v大,这个时候我们将v插入队尾.其实现代码如下: int

poj1442 Black Box【优先队列,定义两个队列】

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 empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are t

【STL】栈+队列+优先队列(详)+ 拯救行动题解

一.栈 栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素又称作进栈.入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素:从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素. 说通俗一点,就是一种有口无肛门的数据结构 咳咳...是一种满足"后进先出"规则的数据结构.有PUSH和POP两种操作.PUSH:把元素压入栈顶 POP:把元

STL 队列 、优先队列、栈 小结

学长说现在基本上可以开始学习STL中一些标准模板了,今天先总结一下 队列.栈.优先队列 1.队列(queue) 先进先出原则,头文件#include <queue>,定义结构queue<类型>名称;queue<int>q.queue<node>q等: 如: struct node { int x; }f; queue<node>q;//结构体类型队列 q.push(f) //将f压入队列的尾部 node t = q.pop()// 弹出队列的第一

寒假2周打卡

2019-02-07 lyd字符串hash 学了Manacher算法 2019-02-08 今天去了趟姥姥家 晚上学了一下kmp算法 2019-02-09 继续学kmp+exkmp 2019-02-10 今天去喝喜酒,今天真的好烦,我不会喝酒,他们都在喝,所以对我有种讨厌的感觉,在酒桌上的感觉就要死了,下次不会再喝这种喜酒. 今天做的题有点少 题:数据分配:优先队列 2019-02-11 做玉蟾宫那题时,发现了还有悬线法,不过看过以后不能完全理解 今日打卡(悬线法)  (洛谷) 1.p1387

初识Treap

Treap,简单的来说就是Tree+Heap,是一颗平衡树,每个节点有两个信息:1.key:当前节点的关键字 :2.fix:当前节点优先级.key满足二叉排序数的性质,即左儿子都比当前节点小,右儿子都比当前节点大(或相等),fix是一个随机的数,满足小根堆(或大根堆)的性质,fix是为了防止Treap退化成链表的简单优化策略. 如下面的一颗Treap: Treap可以进行下面几种操作:插入,查询第k大,旋转,还有其他一些基本操作和一些高级的操作,这里暂不作介绍. 1.插入元素 Treap的关联形

Codeforces Round #420 C

Okabe and Boxes 题意:有2个操作,add x表示往栈里加入一个数x,remove表示从栈里拿出一个数,若要使得出栈的顺序为递增的,那么至少要对栈里面的元素进行多少次重新排序 思路:stack模拟栈,优先队列模拟出栈,每次记录当前最顶上的元素top,用来和当前出栈的数比较,如果不相等,则进行排序,并标记f表示已经是排好序的位置,若相等,则出栈,然后更新top,注意更新top的时候是取优先队列里的还是stack里的数,若当前的位置大于f,则取栈里的元素更新top,否则取优先队列里的数

uva 11995 I Can Guess the Data Structure 数据结构

// uva 11995 数据结构 // 给你一些操作,确定是队列还是栈还是优先队列(数值大的优先级大) // 简单题,练练基础吧相当于 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <c