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 ...
随机推荐
- HTTP 请求报文 响应报文
引言 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发 ...
- SCCM 客户端的修复
1. Stopping the SMS Agent Host service (net stop ccmexec) 2. Stopping the WMI service (net stop winm ...
- JavaScript中的函数表达式
在JavaScript中,函数是个非常重要的对象,函数通常有三种表现形式:函数声明,函数表达式和函数构造器创建的函数. 本文中主要看看函数表达式及其相关的知识点. 函数表达式 首先,看看函数表达式的表 ...
- 报表引擎API开发入门—带参程序数据集
我们今天又来讲讲报表开发的事,上周开的这个系列入门文章也三四天了,浏览量不佳小编甚是悲伤啊,希望大家多多支持我! 一.问题描述 在实际应用中,可能需要根据表名动态地改变数据源,比如在程序数据集中,通过 ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- Java的各种工具类
下面是java的各种工具,包括获取时间和时间比较,检验集合和字符串是否为空和长度大小等等 1 import java.io.BufferedReader; import java.io.File; i ...
- java 解决中文乱码
//1.代码解决 public class LuanMa { public static String getNewString(String luanma){ String result = &qu ...
- JMeter学习(一)工具简单介绍
一.JMeter 介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件, ...
- Java之反射机制
一:基本概念:在Java运行时,对于任意一个类,能否知道这个类对应的属性和方法?对于一个对象,能否知道可以调用它的哪些方法?YES! 这种动态获取类的信息以及动态调用对象的方法的功能来自于Java语言 ...
- HTML 学习笔记 CSS3 (文本效果)
text-shadow 语法 text-shadow : none | <length> none | [<shadow>, ] * <shadow> 或none ...