POJ1442 Black Box堆的應用


今天是第一次寫ACM的博客啦~拖了好久纔開始的說- -

話說開始接觸ACM近半年,才發掘不總結總結太凌亂了,身邊的同級的童鞋也陸陸續續開始寫了,有大牛也寫了幾十篇了,我也不能落後的太多啊

所以從今天起我也要寫啦~

第一次寫就是寫堆- -昨晚看了一晚上。雖然瞭解一些二叉樹型的堆的原理,但是還是懶得手寫堆的函數了,就直接用的STL裏的priority_queue來做,不過第一次寫手非常的生,調用函數也不熟練- -

先貼上一些需要用到的priority_queue的函數:

1.定義:

    priority_queue<data_type,container,functional>

           傳進去的參量依次是數據類型,容器(一般用Vector),比較方式(一般用Operator <,表示最大堆)

   所以最大堆的簡單定義就是priority_queue<int,vector<int>>x;  x即爲所建立的堆

統一的定義方法是用結構,下面做這個題中的代碼就是用結構建的堆,雖然對於這個題很繁瑣,但是對於以後要建立複雜堆的時候有很大幫助

2. x.push(a)把當前元素壓入堆尾

    x.pop()將堆頂元素剔除

    x.top()返回堆頂元素的值,但仍保留才堆中

    [突然發現其實就是隊列裏的函數- -我二了- -,不寫了- -]

3.本題的關鍵就是對堆的動態維護

話不多說,看題

Black Box
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5608 Accepted: 2259

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

題意是在說,依次給一組數A[i],還有一個找值的數組u[j],然後在噹噹前數組元素個數i=u[j]的時候找值,找的是當前數組第j小的值,並輸出。


代碼:

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#define N 30005
using namespace std;


int A[N],u[N];
typedef struct MyStruct
{
	int val;//利用結構建立優先隊列
}MAX_NUM;
MAX_NUM a;
typedef struct MyStruct2
{
	int val;
}MIN_NUM;
MIN_NUM b;
bool operator<(const MAX_NUM &a,const MAX_NUM &b)//重載Operator < 跟原來一樣的是堆頂元素最大
{
	return a.val<b.val;
}
bool operator<(const MIN_NUM &a,const MIN_NUM &b)//跟<恰好相反的是 堆頂元素最小
{
	return a.val>b.val;
}


priority_queue<MAX_NUM,vector<MAX_NUM>>big;//最大堆
priority_queue<MIN_NUM,vector<MIN_NUM>>small;//最小堆


int main()
{
	int m,n,i,j,num_big;
	while (scanf("%d%d",&m,&n)!=EOF)
	{
		for ( i = 1; i <= m; i++)
		{
			scanf("%d",&A[i]);
		}
		j=0;num_big=0;//num_big是記錄最大堆當前元素的個數,用於動態維護(沒用size())
		for ( i = 1; i <=n ; i++)
		{
			scanf("%d",&u[i]);//講u數組的輸入與處理同時進行,其實可以不用建立u[N],直接就用一個變量存就可以了,因爲前面的沒有用
			while (j<u[i])//當 當前壓入堆的元素個數小於u[i]時,不斷壓入新元素直至滿足當前u[i]的個數條件
			{
				j++;
				a.val=A[j];//用結構建堆,這裏麻煩了些- -
				big.push(a);num_big++;				//壓入元素,個數++
				if (!small.empty() && big.top().val> small.top().val)//每加入一個新元素,都要做一次判斷,就是當前最大堆的最大值與最小堆的最小值哪個大,
					//保證從最大堆的堆頂(第k小的值)到最小堆的堆頂(第k+1小的值)是嚴格遞增的,這樣才能保證最大堆的堆頂就是我們要求的結果
						//如果不是的話,交換【但交換完後不一定這兩個元素還在堆頂】,將最大堆的堆頂元素壓入最小堆,反之再做一次
				{
					b.val=big.top().val;
					a.val=small.top().val;
					big.pop();
					small.pop();
					small.push(b);
					big.push(a);
				}
				if (num_big>i)//這裏還有一個維護,就是當前i值我們要求的是第i小的值,所以最大堆元素個數永遠是i個
					//由於每次加新元素都是先向最大堆加,所以有可能最大堆的元素個數超過i,這時就把最大堆堆頂元素取出壓入最小堆
				{
					b.val=big.top().val;
					big.pop();num_big--;
					small.push(b);
				}
			}
			printf("%d\n",big.top().val);
			if (!small.empty())//最後這裏,只要最小堆還有元素,每次i將要++的時候,說明下一次輸出要找第i+1小的元素了
				//所以這裏的維護也是爲了保證最大堆的元素個數永遠保持爲當前i的大小(因爲要循環到下一次,所以這裏的i其實是i+1)
					//所以這裏需要把最小堆的最小值壓到最大堆中,使之在下一次i+1循環的時候滿足條件
			{
				a.val=small.top().val;
				small.pop();
				big.push(a);num_big++;
			}

		}
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章