题意:

输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数。(总数为偶数时输入偏小的)

trick:

分块操作节约时间

AAAAAccepted code:

  1. #define HAVE_STRUCT_TIMESPEC
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. string s;
  5. stack<int>sk;
  6. int num[],block[];
  7. int main(){
  8. ios::sync_with_stdio(false);
  9. cin.tie(NULL);
  10. cout.tie(NULL);
  11. int n;
  12. cin>>n;
  13. int _size=sqrt();
  14. for(int i=;i<=n;++i){
  15. cin>>s;
  16. if(s[]=='u'){
  17. int x;
  18. cin>>x;
  19. sk.push(x);
  20. ++num[x];
  21. ++block[x/_size];
  22. }
  23. else if(s[]=='o')
  24. if(sk.empty())
  25. cout<<"Invalid\n";
  26. else{
  27. int x=sk.top();
  28. sk.pop();
  29. cout<<x<<"\n";
  30. --num[x];
  31. --block[x/_size];
  32. }
  33. else
  34. if(sk.empty())
  35. cout<<"Invalid\n";
  36. else{
  37. int mid=sk.size()/;
  38. if(sk.size()&)
  39. ++mid;
  40. int sum=;
  41. int k;
  42. for(k=;k<=_size&&sum+block[k]<mid;++k)
  43. sum+=block[k];
  44. for(int j=k*_size;j<(k+)*_size;++j){
  45. sum+=num[j];
  46. if(sum>=mid){
  47. cout<<j<<"\n";
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. return ;
  54. }

【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. apache+SSL 搭建https

    简单介绍 一般情况下,我们打开网站默认的是使用明文传输方式,但在日常生活中,当我们在登录或者支付交易时,网站就会自动跳转至SSL(Secure Sockets Layes)加密传输模式,SSL的功能就 ...

  2. C++11 新特性学习

    在Linux下编译C++11 #include<typeinfo> int main() { auto a=; cout<<typeid(a).name()<<en ...

  3. 吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  4. caffe_ocr开源项目学习笔记

    本机配置cuda8.0使用的cudnn是下面要说的重点,vs2015,win10,1080Ti 下载了开源项目:https://github.com/senlinuc/caffe_ocr 编译的时候报 ...

  5. Go_file操作

    1. FileInfo package main import ( "os" "fmt" ) func main() { /* FileInfo:文件信息 in ...

  6. Python面向对象基础语法

    目标 dir 内置函数 定义简单的类(只包含方法) 方法中的 self 参数 初始化方法 内置方法和属性 01. dir 内置函数(知道) 在 Python 中 对象几乎是无所不在的,我们之前学习的  ...

  7. Django 无法同步数据库model相应字段问题

    前言:今天也是充满bug的一天,脸上笑嘻嘻....(继续,讲文明,懂礼貌) 1,问题描述,models中的字段设置的是浮点型,但是输出的结果总是int()类型 models average_score ...

  8. android nfc功能开发

    链接:Android NFC开发详细总结   https://blog.csdn.net/zhwadezh/article/details/79111348 链接2:Android NFC功能 简单实 ...

  9. [QT] QT5.12 HTTPS请求 TLS initialization failed

    #前言 接触到了Qt的网络编程 然后尝试对一个http页面请求获取源码 是可以的 但是当对https界面发出请求的时候总是错误 TLC什么的初始化失败 百度也是没有结果 然后网上各种方法 比如说编译O ...

  10. Go语言基础之rand(随机数)包

    在Golang中,有两个包提供了rand,分别为 "math/rand" 和 "crypto/rand",  对应两种应用场景. "math/rand ...