[POJ1442]Black Box

版权声明:作为一个蒟蒻,转载时请通知我这个蒟蒻 https://blog.csdn.net/zyszlb2003/article/details/89403344

题目描述

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§ <= 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§) sequence.

输入
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.
输出
Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

思路

这道题就是让我们求一个在线数列,对于每个询问 q u e r y [ i ] query[i] 1 i n 1\le i\le n ,求当数列元素个数为 q u e r y [ i ] query[i] ,时第 i i 小的数。
怎么处理这种情况呢?
我们可以开两个堆:
i n in 大根堆
o u t out 小根堆
对于每个询问 q u e r y [ i ] query[i] ,经过操作, o u t out 输出堆顶即可。
操作:

  1. 把还没进队列的数推进 o u t out
  2. 维护一下两个堆,使 i n in 所有元素小于 o u t out 最小(堆顶)元素.
  3. 此时,输出结果后,将 o u t out 最小元素推进 i n in 中,此时, i n in 的元素个数保持 i i (操作完后)个。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
const int N=3e4+10;
struct heap_max
{
	int a[N],len;
	heap_max(){};
	inline void m_swap(int i,int j){int t=a[i];a[i]=a[j];a[j]=t;}
	inline void up(int j)
	{
		while(j!=1)
		{
			int i=j>>1;
			if(a[i]<a[j])m_swap(i,j),j=i;
			else break;
		}
	}
	inline void down(int i)
	{
		int j=i<<1;
		while(j<=len)
		{
			if(j<len&&a[j+1]>a[j])j++;
			if(a[i]<a[j])m_swap(i,j),i=j,j=i<<1;
			else break;
		}
	}
	void ins(int x){a[++len]=x;up(len);}
	inline int top(){return a[1];}
	void pop(){a[1]=a[len--];down(1);}
}in;
struct heap_min
{
	int a[N],len;
	heap_min(){};
	inline void m_swap(int i,int j){int t=a[i];a[i]=a[j];a[j]=t;}
	inline void up(int j)
	{
		while(j!=1)
		{
			int i=j>>1;
			if(a[i]>a[j])m_swap(i,j),j=i;
			else break;
		}
	}
	inline void down(int i)
	{
		int j=i<<1;
		while(j<=len)
		{
			if(j<len&&a[j+1]<a[j])j++;
			if(a[i]>a[j])m_swap(i,j),i=j,j=i<<1;
			else break;
		}
	}
	void ins(int x){a[++len]=x;up(len);}
	inline int top(){return a[1];}
	void pop(){a[1]=a[len--];down(1);}
}out;
int query[N],a[N];
int main()
{
	int n,m,len=1;scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d",&query[i]);
	for(int i=1;i<=m;i++)
	{
		while(len<=query[i])out.ins(a[len++]);
		while(out.top()<in.top()&&in.len)
		{
			int x=out.top(),y=in.top();
			in.pop();out.pop();
			in.ins(x);out.ins(y);
		}
		printf("%d\n",out.top());
		in.ins(out.top());
		out.pop();
	}
	return 0;
}
``

猜你喜欢

转载自blog.csdn.net/zyszlb2003/article/details/89403344
Box