【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 ...
随机推荐
- [USACO13JAN]Cow Lineup
Description Luogu3069 USACO Solution 由于两个点之间最多可以有\(k+1\)种牛,而牛的种数是单调的.所以可以用尺取法(区间伸缩法),每次右移右端点后,让左端点不断 ...
- 当用命令导入csv文件时提示错误[Err] 1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
安装之后没有my.ini配置文件怎么办,因为自己安装的是zip压缩版的mysql,所以再5.7之后就没有my.ini配置文件,所以有时候需要去自己创建一个叫my.ini的配置文件,但是特别 要 ...
- 【转载】C/C++编译过程分析
转自:http://www.360doc.com/content/14/0109/16/835125_343879650.shtml C/C++编译过程 C/C++编译过程主要分为4个过程 1) 编译 ...
- [Leetcode] 攻略计划
在同龄人中我的逻辑思维应该是中下水平,要好好练练.而训练的内容之一,就是解决信息问题,锻炼建模能力. 今天先从队列和栈开始,这两种数据结构大概是计算机和许多程序中最重要的组成部分了.
- List 数据分批入库
直接贴代码,主要运用 List<E> subList(int fromIndex, int toIndex); 把 List 分割 /** * 保存批价结果 * * @param pric ...
- Spring - Spring Boot - 应用构建与运行
概述 spring boot 应用构建 spring boot 应用运行 背景 之前的看了看 Spring 的书, 结果老懒没实践 而且后续有别的想法, 但这个始终是第一步 1. 准备 知识 java ...
- nginx autoindex 配置目录浏览功能
Nginx打开目录浏览功能 yum install httpd-tools -y cd /usr/local/openrestry/nginx/conf/ htpasswd -c passwd adm ...
- uniGUI之文件下载(29)
1]SendFile 2]SendStream 3]自定义类型文件下载 1]SendFile UniSession.SendFile('新建文本文档.txt' //服务器端 文件名 ,'anew.tx ...
- mysql 查询结果保存为表
mysql> create table stunow select distinct 学号,姓名,密码 from stu12to15 ;
- PyCharm安装及汉化设置为中文(附汉化包)
下载:https://www.jetbrains.com/pycharm/download/#section=windows 下载社区版免费 双击运行安装程序 Next 选择安装路径安装 创建桌面快捷 ...