pat1057 stack
超时算法,利用2的特殊性,用2个multiset来维护。单个multiset维护没法立即找到中位数。
其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 7;
int n;
multiset<int> up, lo;
stack<int> sta;
void adapt() {
int cnt = up.size() + lo.size();
int upsz = ceil(cnt / 2.0);
while (up.size() < upsz) {
up.insert(*lo.begin());
lo.erase(lo.begin());
}
while (up.size() > upsz) {
int x = *up.rbegin();
lo.insert(x);
up.erase(up.find(x));
}
}
void push(int x) {
up.insert(x);
adapt();
}
int peek() {
return *(up.rbegin());
}
void pop(int x) {
if (up.find(x) != up.end())
up.erase(up.find(x));
else
lo.erase(lo.find(x));
adapt();
}
int main() {
freopen("in.txt", "r", stdin);
cin >> n;
while (sta.empty() == false)
sta.pop();
up.clear(), lo.clear();
char cmd[13];
int x;
while (n--) {
cin >> cmd;
if (cmd[1] != 'u') {
if (lo.empty() && up.empty()) {
puts("Invalid");
continue;
}
if (cmd[1] == 'o') {
int x = sta.top();
sta.pop();
cout << x << endl;
pop(x);
} else if (cmd[1] == 'e') {
cout << peek() << endl;
}
} else {
cin >> x;
sta.push(x);
push(x);
}
}
return 0;
}
此题正解树状数组
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
;
int c[N];
int lowbit(int i){
return i&(-i);
}
void add(int pos,int value){
while(pos<N){
c[pos]+=value;
pos+=lowbit(pos);
}
}
int sum(int pos){
;
){
res+=c[pos];
pos-=lowbit(pos);
}
return res;
}
int find(int value){
,r=N-,median,res;
){
==)
median=(l+r)/;
else
median=(l+r-)/;
res=sum(median);
if(res<value)
l=median;
else
r=median;
}
;
}
int main(){
//freopen("D://test.txt","r",stdin);
];
,n,pos;
memset(c,,sizeof(c));
scanf("%d",&n);
while(n--){
scanf("%s",ss);
]=='u'){
scanf("%d",&pos);
stack[++top]=pos;
add(pos,);
}]=='o'){
){
printf("Invalid\n");
continue;
}
int out=stack[top];
add();
printf("%d\n",stack[top--]);
}]=='e'){
){
printf("Invalid\n");
continue;
}
int res;
==)
res=find(top/);
else
res=find((top+)/);
printf("%d\n",res);
}else{
printf("Invalid\n");
}
}
;
}
类似题目:zoj3612
pat1057 stack的更多相关文章
- pat1057. Stack (30)
1057. Stack (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Stack is one of ...
- PAT1057 Stack(树状数组+倍增)
目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈 ...
- PAT1057 stack(分块思想)
1057 Stack (30分) Stack is one of the most fundamental data structures, which is based on the princ ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
随机推荐
- ARM指令
语法格式 <opcode>{<cond>}{S} <Rd>, <Rn>,<shifter_operand> {}表示是可选的部分,<& ...
- 自定义SeekBar的使用
一.seekbar是进度条,可以使用系统的,也可以自己定义,下面我们将自己定义一个seekbar. 1.自定义滑条,包括对背景,第一进度,第二进度的设置,通过一个xml来实现,在drawable下创建 ...
- 留念 C语言第一课简单的计算器制作
留念 C语言第一课简单的计算器制作 学C语言这么久了. /* 留念 C语言第一课简单的计算器制作 */ #include<stdio.h> #include<stdlib.h ...
- 【分享】4412开发板ubuntu 12.0.4播放音乐没有声音解决方法
转自迅为论坛:http://bbs.topeetboard.com 准备工作 1.下载 vim 在命令行上输入 apt-get install vim 下载 vim 2.输入 vim /etc/hos ...
- ssh连接失败,排错经验
一.场景描述 ssh连接服务器,发现连接失败,但是对应服务器的ip能够ping通. 场景: [root@yl-web ~]# ssh root@10.1.101.35 ssh_exchange_ide ...
- [转]C#网络编程(订立协议和发送文件) - Part.4
本文转自:http://www.tracefact.net/CSharp-Programming/Network-Programming-Part4.aspx 源码下载:http://www.trac ...
- HDU 5130 Signal Interference --计算几何,多边形与圆的交面积
题意: 求所有满足PB <= k*PA 的P所在区域与多边形的交面积. 解法: 2014广州赛区的银牌题,当时竟然没发现是圆,然后就没做出来,然后就gg了. 圆的一般式方程: 设A(x1,y1) ...
- JMeter学习(十七)JMeter测试Java
目的:对Java程序进行测试 目录 一.核心步骤 二.实例 三.JMeter Java Sampler介绍 四.自带Java Request Sampler 一.核心步骤 1.创建一个Java工程: ...
- linux内核启动以及文件系统的加载过程
Linux 内核启动及文件系统加载过程 当u-boot 开始执行 bootcmd 命令,就进入 Linux 内核启动阶段.普通 Linux 内核的启动过程也可以分为两个阶段.本文以项目中使用的 lin ...
- Ubuntu下初学ROS时所遇小问题
[1]运行命令$ rospack depends1 beginner_tutorials 时,提示 : [rospack] Error: no such package beginner_tutori ...