【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 ...
随机推荐
- apache+SSL 搭建https
简单介绍 一般情况下,我们打开网站默认的是使用明文传输方式,但在日常生活中,当我们在登录或者支付交易时,网站就会自动跳转至SSL(Secure Sockets Layes)加密传输模式,SSL的功能就 ...
- C++11 新特性学习
在Linux下编译C++11 #include<typeinfo> int main() { auto a=; cout<<typeid(a).name()<<en ...
- 吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...
- caffe_ocr开源项目学习笔记
本机配置cuda8.0使用的cudnn是下面要说的重点,vs2015,win10,1080Ti 下载了开源项目:https://github.com/senlinuc/caffe_ocr 编译的时候报 ...
- Go_file操作
1. FileInfo package main import ( "os" "fmt" ) func main() { /* FileInfo:文件信息 in ...
- Python面向对象基础语法
目标 dir 内置函数 定义简单的类(只包含方法) 方法中的 self 参数 初始化方法 内置方法和属性 01. dir 内置函数(知道) 在 Python 中 对象几乎是无所不在的,我们之前学习的 ...
- Django 无法同步数据库model相应字段问题
前言:今天也是充满bug的一天,脸上笑嘻嘻....(继续,讲文明,懂礼貌) 1,问题描述,models中的字段设置的是浮点型,但是输出的结果总是int()类型 models average_score ...
- android nfc功能开发
链接:Android NFC开发详细总结 https://blog.csdn.net/zhwadezh/article/details/79111348 链接2:Android NFC功能 简单实 ...
- [QT] QT5.12 HTTPS请求 TLS initialization failed
#前言 接触到了Qt的网络编程 然后尝试对一个http页面请求获取源码 是可以的 但是当对https界面发出请求的时候总是错误 TLC什么的初始化失败 百度也是没有结果 然后网上各种方法 比如说编译O ...
- Go语言基础之rand(随机数)包
在Golang中,有两个包提供了rand,分别为 "math/rand" 和 "crypto/rand", 对应两种应用场景. "math/rand ...