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. 记一个logrotate的配置文件权限问题

    问题描述 从git仓库更新了别人配置好的logrotate,发现不能正常运行.手工执行报错 error: Ignoring syslog because of bad file mode - must ...

  2. [ python ] 购物系统

    作业需求 1. 购物系统,能够注册登录,用户第一次登录后,让用户输入金额,然后打印商品列表2. 允许用户根据商品编号购买商品3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. 购买完一 ...

  3. HDU 2147 kiki's game(博弈图上找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2147 题目大意:给你一个n*m的棋盘,初始位置为(1,m),两人轮流操作,每次只能向下,左,左下这三个 ...

  4. python基础(9)--递归、二叉算法、多维数组、正则表达式

    1.递归 在函数内部,可以调其他函数,如果一个函数在内部调用它本身,这个函数就是递归函数.递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于裂解 递归算法解决问题的特点: 1)递归是 ...

  5. 自家人不认识自家人——考你一道有趣的Javascript小题目

    今天的内容很简单,给大家分享一个有趣的Javascript小题目. 题目很简单,就是填空: var a = ______; var b = a; alert(a==b); // alert " ...

  6. 使用CLion

    CLion是JetBrains公司的一款C++的IDE.默认使用Cmake构建. ubuntu和fedora下的安装 在ubuntu下安装了CLion,和QtCreator相比: ibus输入法能输入 ...

  7. 百度NLP面试题

    C++ :     1.拷贝构造函数和重载=符分别在什么情况下被调用,实现有什么区别 2.虚函数的目的,虚函数和模板类的区别,如何找到虚函数 常规算法: 1. 如何输出一个集合的所有真子集,递归和非递 ...

  8. 服务器fsockopen函数和pfsockopen函数开启及作用

    摘要: fsockopen()函数的作用是可以用来打开一个socket连接,另一个函数pfsockopen()也有相似的功能,只不过后者是一个“持续”(persistent)的fsockopen()函 ...

  9. jmeter----计数器

    在测试过程中,往往需要一些有一定规则的数字,这个时候,可以使用配置元件中的计数器去实现. 一.界面显示 二.配置说明 1.名称:标识 2.注释:备注 3.启动:是指计数器开始的值 4.递增:每次增加的 ...

  10. 用户代码未处理EntityCommandExecutionmException报错解决方案

    原因可能是(1)没有编译好,清理解决方案,重新生成解决方案.          (2)可能是WebSiteConfiguration.DbProviderName;中为DbProviderName属性 ...