PAT1057 stack(分块思想)

1057 Stack (30分)
 

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 PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print Invalid instead.

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

思路:采用分块的思想,将数据分成数据范围的sqrt(N)块,上取整,每块中的元素个数不超过sqrt(N)下取整,block[i]记录每块含有的元素个数
table[x]记录元素出现的次数

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <stack> 
 5 #include <cmath>
 6 
 7 using namespace std ;
 8 
 9 const int N = 100010,K = 316 ;
10 int block[K+1] ;
11 int table[N] ;
12 
13 stack<int> stk ;
14 
15 int peekMedian(int cnt){
16     int sum = 0 ;
17     if(cnt%2){
18         cnt = (cnt+1)/2 ;
19     }else{
20         cnt /= 2 ;
21     }
22     int i = 0,idx = cnt ;
23     for(;i<K;i++){
24         sum += block[i] ;
25         if(sum>=cnt){
26             break ;
27         }
28         idx -= block[i] ;
29     }
30     for(int j=i*K;j<K*(K+1);j++){
31         if(idx<=table[j]){
32             return j ;
33         }
34         idx -= table[j] ;
35     }
36 }
37 
38 int main(){
39     int n ;
40     cin >> n ;
41     
42     int cnt = 0 ;
43     while(n--){
44         string cmd ;
45         cin >> cmd ;
46         
47         if(cmd == "Pop"){
48             if(stk.empty()){
49                 cout << "Invalid" << endl ;
50             }else{
51                 int ele = stk.top() ;
52                 stk.pop() ;
53                 cout << ele << endl ;
54                 block[ele/K] -- ;
55                 table[ele] -- ;
56                 cnt -- ;    
57             }
58         }else if(cmd == "Push"){
59             int x ;
60             cin >> x ;
61             stk.push(x) ;
62             block[x/K] ++ ;
63             table[x] ++ ;
64             cnt ++ ;
65         }else{
66             if(stk.empty()){
67                 cout << "Invalid" << endl ;
68             }else{
69                 cout << peekMedian(cnt) << endl ;
70             }
71         }
72     }
73     
74     return 0 ;
75     
76 } 

 

...

posted @ 2019-12-16 17:47  gulangyuzzz  阅读(180)  评论(0编辑  收藏  举报