当前位置:网站首页>Pat class a a1057 stack
Pat class a a1057 stack
2022-06-29 02:48:00 【Madness makes freedom】
PAT Class A A1057
1057 Stack (30 branch )
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 (≤10
5
). 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 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:
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
thought : For sequences that update data in real time , The... In a single query sequence K A large number will cause O(n) Time complexity of ,
But you add sorting , This is again. O(n*lgn), Of course , Do not reorder , Update the original sequence directly , A total of
O(n) Time complexity of ;
Block thought : Block the number of queries , Each block stores a certain range of values respectively , Then divided into ceil(n^0.5) block , Each piece
It is divided into floor(n^0.5), perhaps ceil(n^0.5) Number of ranges
hash Array table[i] Represents the current integer i The number of existence ,block[i] It means the first one i The number of elements in a block ;
Algorithm notes :
/**
thought : For sequences that update data in real time , The... In a single query sequence K A large number will cause O(n) Time complexity of ,
But you add sorting , This is again. O(n*lgn), Of course , Do not reorder , Update the original sequence directly , A total of
O(n) Time complexity of ;
Block thought : Block the number of queries , Each block stores a certain range of values respectively , Then divided into ceil(n^0.5) block , Each piece
It is divided into floor(n^0.5), perhaps ceil(n^0.5) Number of ranges
*/
#include <iostream>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
using namespace std;
const int maxn=100001;
const int sqr_n=316;
int every_block_num(int n);// Find the maximum number of each block
void Push(int n);
void Pop();
void PeekMedian();
stack<int> sta;
int table[maxn]; /** table[i] Represents the current integer i The number of existence */
int block[sqr_n+1]; /** block[i] It means the first one i The number of elements in a block */
int main()
{
// cout << 316*316 << endl;
// cout << (99999/316) <<endl;
// while(1);
memset(table,0,sizeof(table));
memset(block,0,sizeof(block));
int query_num,x;
cin >> query_num;
string str;
// cout << ceil(sqrt(n*1.0)) << endl; // Calculate the total number of blocks , How many elements are there in each block
// cout << every_block_num(n) << endl;
for(int i=0;i<query_num;++i)
{
cin >> str;
if(str=="Push")
{
cin >> x;
Push(x);
}
else if(str=="Pop")
{
if(sta.empty())
cout << "Invalid\n";
else
{
Pop();
}
}
else
{
if(sta.empty())
cout << "Invalid\n";
else
{
PeekMedian();
}
}
}
return 0;
}
int every_block_num(int n)
{
double sqr=sqrt(n*1.0);
double floor_sqr=floor(sqr);
double ceil_sqr=ceil(sqr);
if(floor_sqr*ceil_sqr<n)
return ceil_sqr;
else
return floor_sqr;
}
void Push(int n)
{
sta.push(n);
++table[n];
++block[n/sqr_n];
}
void Pop()
{
int top=sta.top();
sta.pop();
--table[top];
--block[top/sqr_n];
cout << top << endl;
}
//void PeekMedian()
//{
// int size=sta.size();
// int k=(size+1)/2;
// int sum=0,i;
// for(i=0;i<316;++i)
// {
// if(sum+block[i]>=k)
// break;
// sum+=block[i];
// }
// int en=(i+1)*316,j;
// for(j=i*316;j<en;++j)
// {
// if(sum+table[j]>=k)
// break;
// sum+=table[j];
// }
// cout << j << endl;
//}
//PeekMedian Another way of writing , use while Control cycle
void PeekMedian()
{
int size=sta.size();
int k=(size+1)/2;
int sum=0,index=0;
while(sum+block[index]<k)
sum+=block[index++];
int j=index*sqr_n;
while(sum+table[j]<k)
sum+=table[j++];
cout << j << endl;
}边栏推荐
- allegro设置网络飞线以及网络颜色的方法
- Has Moore's law come to an end?
- [together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!
- In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
- Target detection - ADAS practice
- The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
- 测试入门——集成测试
- 矩阵特征值和特征向量求解——特征值分解(EVD)
- Kubernetes: container resource requirements and constraints (constraints)
- priority_queue的理解
猜你喜欢

Target detection - ADAS practice

PWN beginner level0

Has Moore's law come to an end?
![[linear algebra] 1.2 total permutation and commutation](/img/04/18fc358c6c426e10c8598bcee9cd43.png)
[linear algebra] 1.2 total permutation and commutation

sql训练01
![[Shangshui Shuo series] the simplest subtitle configuration](/img/22/7e0bcb489d0f2d35c7fe3248ad3419.png)
[Shangshui Shuo series] the simplest subtitle configuration

Leetcode counts the logarithm of points that cannot reach each other in an undirected graph

层次分析法(AHP)

allegro对走好的线取消走线的方法

matlab习题 —— 图像绘制练习
随机推荐
归并排序
50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color
18. `bs对象.节点名.next_sibling` 获取兄弟节点
How does sound amplify weak sounds
Centos7 installation php7.2
安装kibana
In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
PMP项目管理概述
1110: nearest common ancestor (function topic)
Trigonometric function calculation
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
[linear algebra] 1.1 second and third order determinants
After today, I look forward to the new year's eve of the year of the rabbit
快速排序,查询序列的第K大的数
MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year
ArrayList basic operation and add element source code
String length
信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
矩阵特征值和特征向量求解——特征值分解(EVD)