【PAT甲级】1057 Stack (30 分)(分块)
题意:
输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数。(总数为偶数时输入偏小的)
trick:
分块操作节约时间
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string s;
stack<int>sk;
int num[],block[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
int _size=sqrt();
for(int i=;i<=n;++i){
cin>>s;
if(s[]=='u'){
int x;
cin>>x;
sk.push(x);
++num[x];
++block[x/_size];
}
else if(s[]=='o')
if(sk.empty())
cout<<"Invalid\n";
else{
int x=sk.top();
sk.pop();
cout<<x<<"\n";
--num[x];
--block[x/_size];
}
else
if(sk.empty())
cout<<"Invalid\n";
else{
int mid=sk.size()/;
if(sk.size()&)
++mid;
int sum=;
int k;
for(k=;k<=_size&&sum+block[k]<mid;++k)
sum+=block[k];
for(int j=k*_size;j<(k+)*_size;++j){
sum+=num[j];
if(sum>=mid){
cout<<j<<"\n";
break;
}
}
}
}
return ;
}
【PAT甲级】1057 Stack (30 分)(分块)的更多相关文章
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- PAT 甲级 1057 Stack
https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...
- 1057 Stack (30分)(树状数组+二分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
随机推荐
- RTMP服务器搭建(nginx+rtmp)
参考文章:https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.5 ...
- Yum与list结合
我用两台Linux LinuxA IP:192.168.10.101 LinuxB IP:192.168.10.102 首先我们在LinuxA上挂载光驱和安装FTP服务器 然后安装 ...
- 页面在谷歌浏览器和IE浏览器顶部多出空白行,火狐显示正常
引起这个问题的原因是用系统自带记事本打开过Tinkphp的模板文件,因为记事本不支持UTF8无BOM格式编码,打开之后这个编码格式丢失,转为ANSI格式的编码了: 解决的办法是:用Notepad++或 ...
- python正则元字符的含义
练习的时候使用linux+ipython,ipython安装 python的元字符 # 元字符 :# . ^ $ * + ? {} [] \ | () 注:\w ...
- [转] Go 的并发模式:Context
[转] Go 的并发模式:Context tips:昨天看了飞雪无情的关于 Context 的文章,对 go 中 Context 有了一个初步的认识.今天看到一个 go 官方博客的关于 Context ...
- AcWing 852. spfa判断负环 边权可能为负数。
#include <cstring> #include <iostream> #include <algorithm> #include <queue> ...
- 3.CRUD(增删改查)
Select 选择,查询语句 id:就是对应的namespace中的方法名: resultType:Sql语句执行的返回值: parameterType:参数类型 我们想使用查询语句首先要在UserM ...
- 【游戏体验】Infiltraing the Airship(火柴人潜入飞船)
这款作品的游戏性非常高 而且很多地方都是玩梗 不乏趣味和幽默 推荐试玩 个人测评 游戏性 10/10 音乐 9/10 剧情 8/10 总评 27/30
- python的datetime库
datetime 库 简介 获取时间 主要提供程序计时器 基本使用
- 每天进步一点点------基础实验_12_有限状态机 :Moore型序列检测器
/********************************************************************************* * Company : * Eng ...