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 (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.ios

Input Specification:ide

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

Push key
Pop
PeekMediancode

where key is a positive integer no more than 105.orm

Output Specification:blog

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:ip

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:ci

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

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string>
 5 #include<stack>
 6 #include<math.h>
 7 using namespace std;
 8 int hashTB[100001] = {0}, block[1001] = {0};
 9 stack<int> stk;
10 int main(){
11     int N, num;
12     int bk = sqrt(100000.0);
13     string ss;
14     cin >> N;
15     for(int i = 0; i < N; i++){
16         cin >> ss;
17         if(ss == "Pop"){
18             if(stk.empty() == false){
19                 num = stk.top();
20                 cout << num << endl;
21                 stk.pop();
22                 hashTB[num]--;
23                 block[num / bk]--;
24             }else{
25                 cout << "Invalid"  << endl;
26             }
27         }else if(ss == "Push"){
28             cin >> num;
29             stk.push(num);
30             hashTB[num]++;
31             block[num / bk]++;
32         }else if(ss == "PeekMedian"){
33             if(stk.empty() == true){
34                 cout << "Invalid" << endl;
35             }else{
36                 int cnt = stk.size(), j = 0, sum = 0;
37                 if(cnt % 2 == 0)
38                     cnt = cnt / 2;
39                 else cnt = (cnt + 1) / 2;
40                 while(sum < cnt && j < bk){
41                     sum += block[j];
42                     j++;
43                 }
44                 j--;
45                 sum = sum - block[j];
46                 for(j = j * bk; j < 100001; j++){
47                     sum += hashTB[j];
48                     if(sum >= cnt){
49                         cout << j << endl;   
50                         break;
51                     }
52                 }
53             }
54         }
55     }
56     return 0;
57 }
View Code

總結:element

一、超時了三個點,先這樣吧,之後有時間再想一想。用一個stack來同步模擬題中的push與pop操做。另使用一個hash表維護每一個元素出現的次數。須要中位數時,就從頭開始累加次數,直到達到中位數爲止。爲了加快速度,還能夠將hash表分紅N塊,至關於創建一個索引表,存儲每一塊塊內的元素個數。這樣就能夠先搜索塊,次數差很少以後,再進入該塊內搜索。分塊的下標、循環條件等等很差想時,先簡化成只有2塊,每塊50個元素來類比。

二、記得每次提交都要把最後的cin去掉。

更新: 把cin cout string 換成 scanf printf 和 char [ ] 居然全過了。看來仍是能不用cin cout就不用啊。下面是最新的代碼。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string>
 5 #include<stack>
 6 #include<math.h>
 7 using namespace std;
 8 int hashTB[100001] = {0}, block[1001] = {0};
 9 stack<int> stk;
10 int main(){
11     int N, num;
12     int bk = sqrt(100000.0);
13     char ss[100];
14     scanf("%d", &N);
15     for(int i = 0; i < N; i++){
16         scanf("%s", ss);
17         if(ss[1] == 'o'){
18             if(stk.empty() == false){
19                 num = stk.top();
20                 printf("%d\n", num);
21                 stk.pop();
22                 hashTB[num]--;
23                 block[num / bk]--;
24             }else{
25                 printf("Invalid\n");
26             }
27         }else if(ss[1] == 'u'){
28             scanf("%d", &num);
29             stk.push(num);
30             hashTB[num]++;
31             block[num / bk]++;
32         }else if(ss[1] == 'e'){
33             if(stk.empty() == true){
34                 printf("Invalid\n");
35             }else{
36                 int cnt = stk.size(), j = 0, sum = 0;
37                 if(cnt % 2 == 0)
38                     cnt = cnt / 2;
39                 else cnt = (cnt + 1) / 2;
40                 while(sum < cnt && j < bk){
41                     sum += block[j];
42                     j++;
43                 }
44                 j--;
45                 sum = sum - block[j];
46                 for(j = j * bk; j < 100001; j++){
47                     sum += hashTB[j];
48                     if(sum >= cnt){
49                         printf("%d\n", j);
50                         break;
51                     }
52                 }
53             }
54         }
55     }
56     cin >> num;
57     return 0;
58 }
View Code
相關文章
相關標籤/搜索