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的更多相关文章

  1. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  2. pat 甲级 1057 Stack(30) (树状数组+二分)

    1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...

  3. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  4. PAT 甲级 1057 Stack

    https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...

  5. PAT甲级——A1057 Stack

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  6. PAT 1057 Stack [难][树状数组]

    1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...

  7. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. Linux 内核进程管理之进程ID【转】

    转自:http://www.cnblogs.com/hazir/p/linux_kernel_pid.html Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构, ...

  2. mysql的一些规范

    我司的mysql规范,值得牢记 一.禁止内容 表字段类型 不推荐使用enum,set,blob,text等类型 表中所有字段都不能为空,需要设置not null属性,可以给默认值 表的主键列值禁止被更 ...

  3. React 16 源码瞎几把解读 【三 点 二】 react中的fiberRoot

    〇.先来看看常用的常量 NoWork = 0 noTimeout = undefined HostRoot = 3 NoContext = 0b000; AsyncMode = 0b001; Stri ...

  4. 匿名函数、lambda表达式

    匿名函数 func = lambda x: y #x是形参,y是返回值 键字lambda表示匿名函数,冒号前面的x表示函数参数,冒号后面的y表示匿名函数的返回值. 例1:返回列表中长度大于等于3的元素 ...

  5. VS2015_动态链接库学习

    非MFC动态链接库 创建一个名为ex1的Win32项目 创建一个DLL项目,保留预编译的头文件   默认文件 创建完成项目之后,包含几个默认的文件   stdafx.h文件用于包含标准系统包含的头文件 ...

  6. C/C++——[02] 运算符和表达式

    C/C++中表示数据运算的符号称为“运算符”.运算符所用到的操作数个数,称为运算符的“目数”. C/C++语言的运算符有赋值运算符.算术运算符.逻辑运算符.位运算符等多类. 将变量.常量等用运算符连接 ...

  7. Morris Traversal方法遍历

    实现二叉树的遍历且只需要O(1)的空间. 参考:http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html

  8. HDU 2822

    Dogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8

  10. Mysql聚合函数count(*) 的性能分析

    你首先要明确的是,在不同的 MySQL 引擎中,count(*) 有不同的实现方式. MyISAM 引擎把一个表的总行数存在了磁盘上,因此执行 count(*) 的时候会直接返回这个数,效率很高: 而 ...