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 ...
随机推荐
- 解决内网主机ping不通网关能ping内网
有一台笔记本电脑可以自动获取IP,可以和内网其他主机互相PING通,就是PING 不通网关,只能上内网,不能上外网,IP换到其他主机上也可以上外网,说明路由器上没什么限制.路由器也查了,电脑也重装了, ...
- 在Eclipse里查看Java字节码
要理解 Java 字节码,比较推荐的方法是自己尝试编写源码对照字节码学习.其中阅读 Java 字节码的工具必不可少.虽然javap可以以可读的形式展示出.class 文件中字节码,但每次改动源码都需调 ...
- java 正则表达式的应用:读取文件,获取其中的电话号码
1.正则表达式 正则表达式,又称正规表示法.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式使用单个字符串来描 ...
- GNU C 内联汇编介绍
GNU C 内联汇编介绍 简介 1.很早之前就听说 C 语言能够直接内嵌汇编指令.但是之前始终没有去详细了解过.最近由于某种需求,看到了相关的 C 语言代码.也就自然去简单的学习了一下如何在 C 代码 ...
- HashMap和HashSet
Java使用Set接口来描述集合,而Set中每一个数据元素都是唯一的. HashSet散列集合 Hash算法:把任意长度输入,通过散列算法,变换成固定长度的输出即散列值.对不同类型信息,散列值公式也是 ...
- Stanford机器学习笔记-10. 降维(Dimensionality Reduction)
10. Dimensionality Reduction Content 10. Dimensionality Reduction 10.1 Motivation 10.1.1 Motivation ...
- GTAC 2015将于11月10号和11号召开
今年的GTAC注册已经结束,将会在11月10号和11号在Google马萨诸塞州剑桥办公室召开.大家可以关注https://developers.google.com/google-test-autom ...
- AC日记——密码翻译 openjudge 1.7 09
09:密码翻译 总时间限制: 1000ms 内存限制: 65536kB 描述 在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,但仍 ...
- MIT License
早上看到微软的UWP例子,在代码里看到 Copyright (c) Microsoft. All rights reserved.// This code is licensed under the ...
- tyvj[1089]smrtfun
描述 现有N个物品,第i个物品有两个属性A_i和B_i.在其中选取若干个物品,使得sum{A_i + B_i}最大,同时sum{A_i},sum{B_i}均非负(sum{}表示求和). 输入格式 ...