当前位置:网站首页>PAT (Advanced Level) Practice 1057 Stack
PAT (Advanced Level) Practice 1057 Stack
2022-07-01 06:28:00 【Keep--Silent】
List of articles
subject
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
General description
Three operations :
- Insert x
- Delete x
- Query median
Their thinking
data size 1 0 5 10^5 105, So give up , So we need some specific structures , The complexity of the three operations that require a single insert and delete query must be less than n n n, So that the overall complexity does not exceed n 2 n^2 n2, So choose a tree array .
Insert and delete
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;
}
}
- Insert x: update(x,1);
- Delete x: update(x,-1);
therefore :
- Insert x
- Delete x
- Query median
Query median
The tree array represents the prefix and , We use it vt Storage quantity , Two points search that will do : That is, we are looking for the median , I.e. find The first position equal to or greater than half of the quantity .
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);
}
thus
- Insert x
- Delete x
- Query median
Complexity analysis
Dichotomous complexity log n \log n logn,getsum Complexity log n \log n logn, in total n n n operations , Overall complexity n log 2 n n \log^2 n nlog2n
Complete code
#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;
}
边栏推荐
- 第五章 輸入/輸出(I/O)管理
- Dongle data collection
- C#如何打印输出原版数组
- ForkJoin和Stream流测试
- HCM Beginner (II) - information type
- SQL语句
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
- Chapter V input / output (i/o) management
- JMM details
- JSON module
猜你喜欢
![[unity shader ablation effect _ case sharing]](/img/e3/464f1cf426e8c03ce3d538ed9cf4bc.png)
[unity shader ablation effect _ case sharing]

ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)

Free trial of self-developed software noisecreater1.1

HCM Beginner (IV) - time

JMM details
![[enterprise data security] upgrade backup strategy to ensure enterprise data security](/img/59/e44c6533aa546e8854ef434aa64113.png)
[enterprise data security] upgrade backup strategy to ensure enterprise data security

高阶-二叉平衡树

【KV260】利用XADC生成芯片温度曲线图
![[ManageEngine] how to realize network automatic operation and maintenance](/img/8a/75332d3180f92c6a6482d881032bbf.png)
[ManageEngine] how to realize network automatic operation and maintenance

SQL学习笔记九种连接2
随机推荐
To sort out the anomaly detection methods, just read this article!
[leetcode] day91- duplicate elements exist
局域网监控软件有哪些功能
[unity shader amplify shader editor (ASE) Chapter 9]
C语言课设工资管理系统(大作业)
VS2019如何永久配置本地OpenCV4.5.5使用
sci-hub如何使用
SQL语言的学习记录一
Gson的@JsonAdater注解的几种方式
Uniapp tree level selector
谷粒商城-环境(p1-p27)
Draw a directed graph based on input
Idea easy to use plug-in summary!!!
[ManageEngine Zhuohao] mobile terminal management solution, helping the digital transformation of Zhongzhou aviation industry
[unity shader custom material panel part I]
嵌入式系统
idea 好用插件汇总!!!
[ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
10 golang operator
FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview