A1057 Stack (30 point(s))

stack栈使用,求第K大的数:块状数组/树状数组

1. 原文

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 Nelements, 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 (≤ $10^5$ ). Then N lines follow, each contains a command in one of the following 3 formats:

1
2
3
Push key
Pop
PeekMedian

where key is a positive integer no more than $10^5$.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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:

1
2
3
4
5
6
7
8
9
10
11
12
Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

2. 解析

块状数组:

对应块 block[x/num]++ block[x/num]–

求第k块:while(sum+block[idx]<k) while(sum+table[ans]<k)

树状数组:

lowbits(i) (i&(-i)) 能整除i的最大2次幂

对应加入 for (int i = x; i < maxn; i+=lowbits(i))

删除 for (int i = x; i > 0; i-=lowbits(i))

求k大:两分法

3. AC代码 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<cstdio>
#include<stack>
#include<cstring>
#define lowbits(i) ((i)&(-i))
using namespace std;
const int maxn=100010;
char str[10];
int c[maxn]={};
stack<int> s;
void update(int x,int num){
for (int i = x; i < maxn; i+=lowbits(i))
{
c[i]+=num;
}
}
int getsum(int x){
int sum=0;
for (int i = x; i > 0 ; i-=lowbits(i))
{
sum+=c[i];
}
return sum;
}
void peekmedian(){
int left=1; int right=maxn; int k=(1+(int)s.size())/2;
while(left<right){
int mid=(left+right)/2;
if (getsum(mid)>=k)
{
right=mid;
}else{
left=mid+1;
}
}
printf("%d\n",left);
}
int main()
{
int n;
scanf("%d",&n);
int x;
for (int i = 0; i < n; ++i)
{
scanf("%s",str);
if (strcmp(str,"Pop")==0)
{
if (s.empty())
{
printf("Invalid\n");
}else{
printf("%d\n",s.top());
update(s.top(),-1);
s.pop();
}
}else if(strcmp(str,"PeekMedian")==0){
if (s.empty())
{
printf("Invalid\n");
}else{
peekmedian();
}
}else{
scanf("%d",&x);
s.push(x);
update(x,1);
}

}

return 0;
}

4. AC代码2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int num=316;
char str[10];
stack<int> s;
int block[317]={};
int table[100010]={};
void push(int x){
block[x/num]++;
table[x]++;
}
void pop(int x){
block[x/num]--;
table[x]--;
}
void peekmedian(){
int idx=0; int sum=0;
int k=(1+(int)s.size())/2;
while(sum+block[idx]<k){
sum+=block[idx++];
}
int ans=idx*num;
while(sum+table[ans]<k){
sum+=table[ans++];
}
printf("%d\n", ans);
}
int main()
{
int n;
scanf("%d",&n);
int x;
for (int i = 0; i < n; ++i)
{
scanf("%s",str);
if (strcmp(str,"Pop")==0)
{
if (s.empty())
{
printf("Invalid\n");
}else{
printf("%d\n",s.top());
pop(s.top());
s.pop();
}
}else if(strcmp(str,"PeekMedian")==0){
if (s.empty())
{
printf("Invalid\n");
}else{
peekmedian();
}
}else{
scanf("%d",&x);
s.push(x);
push(x);
}
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1057/
  • 发布时间: 2019-09-03 12:33
  • 更新时间: 2021-10-29 14:02
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!