PAT甲级——A1057 Stack

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (-th smallest element if N is even, or (-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

where key is a positive integer no more than 1.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMediancommand, print in a line the corresponding returned value. If the command is invalid, print Invalidinstead.

Sample Input:

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Sample Output:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

数组数据是随时都在进行输入与删除的,让你实时反馈数组中的中位数:
使用分块思想
1、一般来说,为了达到高效率的目的,对一个有N个元素的有序序列来说,除最后一块外,其余每块中元素的个数都应当为 | √N | (此处为向下取整,方便程序实现),于是块数为[√N](此处为向上取整)。这样就把有序序列划分为[√N]块,其中每块中元素的个数不超过 | √N | 。
考虑到序列中的元素都是不超过105的非负整数,因此不妨设置一个hash数组table[100001],其中table[x]表示整数x的当前存在个数;
接着,借助分块思想,从逻辑上将0~10分为 | √(105 + 1) |= 317块,其中每块的元素个数为316。逻辑上进行分块的结果如下:
0,1,2.…,314,315为第0块;
316,317…,630,631为第1块。
99856,99857,…,100000为第316块。
这样分块有什么用呢?可以定义一个统计数组block[317],其中block[i]表示第i块中存在的元素个数。于是假如要新增一个元素x,就可以先计算出x所在的块号为x / 316,然后让block[x / 316]加1,表示该块中元素个数多了1;同时令table[x]加1,表示整数x的当前存
在个数多了1。
例如想要新增334这个元素,就可以通过334 / 316 = 1算出元素334所在的块号为1,然后令block[1]++,表示1号块增加了一个元素,并令table[334] + 1,表示元素334的存在个数多了1。
同理,如果想要删除一个元素x,只需要让block[x / 316]和table[x]都减1即可。显然,新增与删除元素的时间复杂度都是O(1)。
接着来看如何查询序列中第K大的元素是什么。
首先,从小到大枚举块号,利用block数组累加得到前i - 1块中存在的元素总个数,然后判断加入i号块的元素个数后元素总个数能否达到K。如果能,则说明第K大的数就在当前枚举的这个块中,此时只需从小到大遍历该块中的每个元素(其中i号块的第一个元素是i * 316),利用table数组继续累加元素的存在个数,直到总累计数达到K,则说明找到了序列第K大的数。显然整体思路是先用O(√N)的时间复杂度找到第K大的元素在哪一块,然后再用0(√N)的时间复杂度在块内找到这个元素,因此单次查询的总时间复杂度为0(√N)。

 1 #include <iostream>
 2 #include <stack>
 3 #include <string>
 4 #include <cmath>
 5 using namespace std;
 6 int N, num, table[100010], block[316];//数的个数,块中数的个数
 7 int main()
 8 {
 9     cin >> N;
10     stack<int>s;
11     string str;
12     for (int i = 0; i < N; ++i)
13     {
14         cin >> str;
15         if (str == "Pop")
16         {
17             if (s.size() > 0)
18             {
19                 cout << s.top() << endl;
20                 table[s.top()]--;
21                 block[s.top() / 316]--;                
22                 s.pop();
23             }
24             else
25                 cout << "Invalid" << endl;
26         }
27         else if (str == "Push")
28         {
29             cin >> num;
30             s.push(num);
31             table[num]++;
32             block[num / 316]++;
33         }
34         else
35         {
36             if (s.size() > 0)
37             {
38                 int mid = s.size() % 2 == 0 ? s.size() / 2 : (s.size() + 1) / 2;
39                 int t = 0, k = 0;
40                 while (k + block[t] < mid) k += block[t++];
41                 int num = t * 316;
42                 while (k + table[num] < mid)k += table[num++];
43                 cout << num << endl;
44             }
45             else
46                 cout << "Invalid" << endl;
47         }
48     }
49     return 0;    
50 }

猜你喜欢

转载自www.cnblogs.com/zzw1024/p/11291758.html