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.

Input Specification:

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:

Push key
Pop
PeekMedian

where key is a positive integer no more than 105.

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

 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

总结:

1、超时了三个点,先这样吧,以后有时间再想想。用一个stack来同步模拟题中的push与pop操作。另使用一个hash表维护每个元素出现的次数。需要中位数时,就从头开始累加次数,直到达到中位数为止。为了加快速度,还可以将hash表分成N块,相当于建立一个索引表,存储每一块块内的元素个数。这样就可以先搜索块,次数差不多之后,再进入该块内搜索。分块的下标、循环条件等等不好想时,先简化成只有2块,每块50个元素来类比。

2、记得每次提交都要把最后的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
posted @ 2018-02-25 01:20  ZHUQW  阅读(121)  评论(0编辑  收藏  举报