题意:

输入一个正整数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 分)(分块)的更多相关文章

  1. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  2. pat 甲级 1057 Stack(30) (树状数组+二分)

    1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...

  3. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

  4. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

  5. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  6. PAT 甲级 1057 Stack

    https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...

  7. 1057 Stack (30分)(树状数组+二分)

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  8. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  9. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

随机推荐

  1. 软件工程2020第一次作业(by cybersa)

    1 作业描述 作业属于哪个课程 2020春福大软工实践W班 这个作业要求在哪里 寒假作业(1/2) 这个作业的目标 建立博客.掌握markdown语法,学习写博客,回顾,总结,展望自己的学习历程 作业 ...

  2. tomcat8.5优化配置

    参考文章: https://www.cnblogs.com/steven-snow/p/9262025.html 1.Tomcat内存使用调整 windows系统在bin/catalina.bat文件 ...

  3. noobSTL-1-配置器-2

    noobSTL-1-配置器-2 第一级配置器 1. 功能分析 第一级配置器以malloc(),free(),realloc()等C语言函数来执行实际的内存配置. 使用C语言函数是因为C++并未直接提供 ...

  4. 【做题笔记】P1042 乒乓球

    坑 #1:输入有若干行,但处理的时候要看成一个整体的信息.比如说第一行最后一局比分是 2:1 ,这时不算比完,这个比分要继承到第二行的信息中继续处理. 坑 #2:一局结束,当且仅当其中一方比分大于等于 ...

  5. 使用SqlDataReader的查询操作

    原创weixin_42430576 发布于2019-01-31 18:49:41 阅读数 762  收藏 展开 using System; using System.Collections.Gener ...

  6. Jmeter学习之-聚合报告详解

    在对接口进行性能测试时,经常用到jmeter的聚合报告来分析数据 首先在待测试的接口或线程组下添加“监听器>聚合报告” 执行请求后,将生成聚合报告,聚合报告包括以下结果参数(中文版+英文版) A ...

  7. 左偏树(p1456) 比较模板的一道题

    题意:有n只猴子,m个操作,每一个操作,会让这两堆猴子里的最大的两只打架,打完之后,自身权值减半,然后他们会成为朋友 也就是会属于同一棵树,细节:如果选出的猴子在同一堆,就输出-1,然后下一个操作,不 ...

  8. [单片机] ESP8266 开机自动透传

    AT+CWMODE=1//设置WiFi工作在透传模式 AT+CWJAP_DEF="XX","YYY"//设置要链接的wifi名称.密码,并进行连接 //设置TC ...

  9. Codeforces 1315C Restoring Permutation

    You are given a sequence b1,b2,…,bnb1,b2,…,bn . Find the lexicographically minimal permutation a1,a2 ...

  10. boolean类型set、get方法

    今天在了解lombok的时候偶然看到一个问题,在bean中存在boolean类型的数据的时候,用eclipse工具自动生成的set.get方法存在的问题. 不管变量为isXXX还是XXX时,set.g ...