PAT甲级1057. Stack
PAT甲级1057. Stack
题意:
堆栈是最基础的数据结构之一,它基于“先进先出”(LIFO)的原理。基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素)。现在你应该实现一个额外的操作堆栈:PeekMedian -
返回堆栈中所有元素的中间值。对于N个元素,如果N是偶数,则将中值定义为(N / 2)个最小元素,或者如果N是奇数则将其定义为((N + 1)/ 2)。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含正整数N(<= 105)。然后N行跟随,
每个都包含以下3种格式之一的命令:
按键
流行的
PeekMedian
其中key是正整数,不超过105。
输出规格:
对于每个Push命令,将密钥插入堆栈并输出任何内容。对于每个Pop或PeekMedian命令,在一行中打印相应的返回值。如果命令无效,
打印“无效”。
思路:
树状数组 参考于:
PAT1057.Stack (30)
树状数组
ac代码:
C++
// pat1057.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
using namespace std;
//树形数组
const int maxn = 1e5 + 5;
int c[maxn];
int lowbit(int i) //count 2^k, (k 为数字二进制末尾0的个数)
{
return i & (-i);
}
void add(int pos, int value)
{
while (pos < maxn)
{
c[pos] += value;
pos += lowbit(pos);
}
}
int sum(int pos)
{
int res = 0;
while (pos > 0)
{
res += c[pos];
pos -= lowbit(pos);
}
return res;
}
int find(int value)
{
int l = 0, r = maxn - 1, median, res;
while (l < r - 1)
{
if ((l + r) % 2 == 0)
{
median = (l + r) / 2;
}
else
{
median = (l + r - 1) / 2;
}
res = sum(median);
if (res < value)
{
l = median;
}
else
{
r = median;
}
}
return l + 1;
}
int main()
{
int n;
scanf("%d", &n);
int stack[maxn], top = 0, pos;
memset(c, 0, sizeof(c));
char oper[20];
while(n--)
{
scanf("%s", oper);
if (oper[1] == 'o') //pop
{
if (top == 0)
{
printf("Invalid\n");
continue;
}
int out = stack[top];
add(out, -1);
printf("%d\n", stack[top--]);
}
else if (oper[1] == 'e') //peekmedian
{
if (top == 0)
{
printf("Invalid\n");
continue;
}
int res;
if (top % 2 == 0) res = find(top / 2);
else res = find((top + 1) / 2);
printf("%d\n", res);
}
else if (oper[1] == 'u') //push
{
scanf("%d", &pos);
stack[++top] = pos;
add(pos, 1);
}
else
{
printf("Invalid\n");
}
}
return 0;
}
PAT甲级1057. Stack的更多相关文章
- 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【树状数组】【二分】
题目: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 ...
- PAT甲级——A1057 Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- PAT 1057 Stack [难][树状数组]
1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
随机推荐
- 健身VS不健身,完全是两种不同的人生!
这两天一组同龄人合照 刷爆了国内健身圈, 图左是一位67岁的老人, 图右是67岁的健美运动员杨新民老师 相同年龄, 但从外观上有着强烈的距离感! 让多人不禁感叹,健身和不健身, 简直就是两种状态,两种 ...
- 2017 CERC
2017 CERC Problem A:Assignment Algorithm 题目描述:按照规则安排在飞机上的座位. solution 模拟. 时间复杂度:\(O(nm)\) Problem B: ...
- js固定小数位数 .toFixed()
toFixed(num)法可把 Number 四舍五入为指定小数位数的数字. num为需要固定的位数 var num=2;console.log(num.toFixed(2));//2.00;var ...
- str.format() 格式化字符串函数
语法 它通过{}和:来代替%. “映射”示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.form ...
- 创建一个简单的Maven工程
Maven的工程结构如下图所示: 大致来看,Maven的工程结构如下: 在创建maven工程时,可以通过骨架创建,也可以不通过骨架创建. 我们先用idea通过骨架创建一个Maven工程. 配置pom. ...
- Linux 基础——常用的Linux网络命令
一.学Linux网络命令有什么好处 网络的出现,我们的生活更方便了,处理事情的效率也越来越高,也可以看到全世界文化的差异.同时我们接受新事物的信息越来越来强,新事物的信息也越来越来多.网络对于我们尔等 ...
- 号外,号外 -几乎所有的binary search和mergesort都有错
号外,号外 -几乎所有的binary search和mergesort都有错 这是Joshua Bloch(Effective Java的作者)在google blog上发的帖子.在说这个帖子之前,不 ...
- 开发 ASP.NET vNext 初步总结(使用Visual Studio 2015 Preview )
新特性: vNext又称MVC 6.0,不再需要依赖System.Web,占用的内存大大减少(从前无论是多么简单的一个请求,System.Web本身就要占用31KB内存). 可以self-host模式 ...
- 三年.NET即将转Java,我该何去何从
2014年5月,大三报了某培训班5个月学习.NET 2014年12月-2015年6月,在某软件公司实习,用ASP.NET开发企业级系统 2015年7月-2017年3月,从毕业生到成为该公司的主要开发人 ...
- VIM配置示例
以下是我习惯的vim配置,做个记录~_~ " 文件编码 set fileencoding=utf- set encoding=utf- set termencoding=utf- " ...