当前位置:网站首页>PAT (Advanced Level) Practice 1057 Stack
PAT (Advanced Level) Practice 1057 Stack
2022-07-01 06:25:00 【Keep--Silent】
题目
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 NNN elements, the median value is defined to be the (N/2)(N/2)(N/2)-th smallest element if NNN is even, or ((N+1)/2)((N+1)/2)((N+1)/2)-th if NNN is odd.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer NNN (≤105\le 10^5≤105). Then NNN 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 10510^5105.
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
大意说明
三种操作:
- 插入x
- 删除x
- 查询中位数
解题思路
数据大小 1 0 5 10^5 105,所以放弃,所以需要一些特定的结构,需要单次插入删除查询三种操作的复杂度必须小于 n n n,才能使得总体复杂度不超过 n 2 n^2 n2,所以选择树状数组。
插入与删除
const int Size = 1e5 + 10;
int vt[Size], n = Size - 5;;
int lb(int x) {
return x & (-x); }
void update(int x,int size) {
for (int i = x; i <= n; i+=lb(i)) {
vt[i]+=size;
}
}
- 插入x: update(x,1);
- 删除x: update(x,-1);
所以:
- 插入x
- 删除x
- 查询中位数
查询中位数
树状数组表示的是前缀和,我们用vt存储数量 ,二分查找即可: 即我们要查找的是中位数,即查找第一个大于等于一半数量的位置。
int getsum(int index) {
int cnt = 0;
for (int i = index; i; i -= lb(i))cnt += vt[i];
return cnt;
}
void Medeian() {
int cnt = getsum(n);
int pot = (cnt+1) / 2;
if (cnt== 0) {
printf("Invalid\n");
return;
}
int l = 1, r = n;
while (l < r) {
int mid = l + r >> 1;
if (getsum(mid) >= pot)r = mid;
else l = mid + 1;
}
printf("%d\n",l);
}
至此
- 插入x
- 删除x
- 查询中位数
复杂度分析
二分复杂度 log n \log n logn,getsum复杂度 log n \log n logn,总共 n n n次操作 ,即总体复杂度 n log 2 n n \log^2 n nlog2n
完整代码
#include <bits/stdc++.h>
using namespace std;
const int Size = 1e5 + 10;
int vt[Size], n = Size - 5;;
int lb(int x) {
return x & (-x); }
void update(int x,int size) {
for (int i = x; i <= n; i+=lb(i)) {
vt[i]+=size;
}
}
int getsum(int index) {
int cnt = 0;
for (int i = index; i; i -= lb(i))cnt += vt[i];
return cnt;
}
void Medeian() {
int cnt = getsum(n);
int pot = (cnt+1) / 2;
if (cnt== 0) {
printf("Invalid\n");
return;
}
int l = 1, r = n;
while (l < r) {
int mid = l + r >> 1;
if (getsum(mid) >= pot)r = mid;
else l = mid + 1;
}
printf("%d\n",l);
}
int main() {
stack<int>sk;
int n,x;
cin >> n;
char ss[300];
while (n--) {
scanf("%s", &ss);
if (ss[1] == 'o') {
if (sk.size() == 0) {
printf("Invalid\n");
continue;
}
x = sk.top();
update(x, -1);
sk.pop();
printf("%d\n", x);
}
else if (ss[1] == 'u') {
scanf("%d", &x);
sk.push(x);
update(x, 1);
}
else {
Medeian();
}
}
return 0;
}
边栏推荐
- [ManageEngine] how to realize network automatic operation and maintenance
- 【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
- Camouflage request header Library: Anti useragent
- [ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
- Detailed steps for installing redis on Windows system
- C语言课设销售管理系统设计(大作业)
- C language course set up salary management system (big homework)
- C language course is provided with employee information management system (large operation)
- 地宮取寶(記憶化深搜)
- 华福证券开户是安全可靠的么?怎么开华福证券账户
猜你喜欢

C语言课设学生选修课程系统(大作业)

Distributed lock implementation

idea 好用插件汇总!!!

【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全

C语言课设销售管理系统设计(大作业)

HCM Beginner (IV) - time

记磁盘扇区损坏导致的Mysql故障排查

C语言课设工资管理系统(大作业)

How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?
![[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage](/img/54/f187e22ad69f3985d30376bad1fa03.png)
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
随机推荐
Minio error correction code, construction and startup of distributed Minio cluster
IT服务管理(ITSM)在高等教育领域的应用
[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform
HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
【KV260】利用XADC生成芯片温度曲线图
json模块
【ManageEngine】如何实现网络自动化运维
Pol8901 LVDS to Mipi DSI supports rotating image processing chip
C语言课设工资管理系统(大作业)
HCM Beginner (I) - Introduction
【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
Kubedm builds kubenetes cluster (Personal Learning version)
C language course set up salary management system (big homework)
C#如何打印輸出原版數組
C语言课设学生考勤系统(大作业)
地宮取寶(記憶化深搜)
端口扫描工具对企业有什么帮助?
[ManageEngine Zhuohao] helps Julia college, the world's top Conservatory of music, improve terminal security
SystemVerilog learning-06-class encapsulation
Teach you how to implement a deep learning framework