1057. Stack (30)
分析:
考察树状数组 + 二分, 注意以下几点:
1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法:
(1):每次全部退栈,进行排序,太浪费时间,不可取。
(2):题目告诉我们key不会超过10^5,我们可以想到用数组来标记,但不支持快速的统计操作。
(3):然后将数组转为树状数组,可以快速的统计,再配上二分就OK了。
2.二分中我们需要查找的是一点pos,sum(pos)正好是当前个数的一半,而sum(pos - 1)就不满足。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <stack>
#include <map> using namespace std; const int Max_required = ; int tree_array[Max_required]; inline int lowbit(int x)
{
return x&(-x);
} void add(int x, int value)
{
while (x < Max_required)
{
tree_array[x] += value;
x += lowbit(x);
}
} int sum(int x)
{
int total = ;
while (x > )
{
total += tree_array[x];
x -= lowbit(x);
}
return total;
} int binary_find(int x)
{
int low = , high = Max_required, mid; while (low <= high)
{
mid = (low + high) >> ;
int total = sum(mid); if (total >= x)
high = mid - ;
else if (total < x)
low = mid + ;
}
return low;
} int main()
{
int n;
stack<int> st;
while (scanf("%d", &n) != EOF)
{
//st.clear();
memset(tree_array, , sizeof(tree_array)); char ch[];
while (n--)
{
scanf("%s", ch);
if (strcmp("Push", ch) == )
{
int pp;
scanf("%d", &pp);
st.push(pp);
add(pp, );
}
else if (strcmp("Pop", ch) == )
{
if (st.empty())
printf("Invalid\n");
else
{
printf("%d\n", st.top());
add(st.top(), -);
st.pop();
}
}
else if (strcmp("PeekMedian", ch) == )
{
int len = st.size();
if (len == ) {
printf("Invalid\n");
continue;
}
int res = -;
if (len % == )
res = binary_find((len + ) / );
else
res = binary_find(len / );
printf("%d\n", res);
}
}
}
return ;
}
1057. Stack (30)的更多相关文章
- 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 (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
- 1057 Stack (30)(30 分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 【PAT甲级】1057 Stack (30 分)(分块)
题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...
- 1057 Stack (30分)(树状数组+二分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
随机推荐
- [ActionScript 3.0] AS3.0 复制xml,并赋值
var template:XML= <person><name><first>FIRST</first><last /></name& ...
- WPF绑定数据源
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Comp ...
- eclipse中提示HttpServletRequest不能引用的解决办法
两种解决方法: 1.右键点击项目->Build Path->Add Libraries..->Server Runtime 选择Apache Tomcat v8.0 2.右键点击项目 ...
- Chap5: question 35 - 37
35. 第一个只出现一次的字符 char firtNotRepeat(char *s) { if(s == NULL) return 0; int i = 0; while(s[i] != '\0') ...
- Codeforces Round #229 (Div. 2) D
D. Inna and Sweet Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 读取ini配置文件
http://blog.sina.com.cn/s/blog_4d11e5f20100fm2s.html c程序有两种方式传入参数到执行文件中:1.运行exe时,直接输入参数:ping.exe 10. ...
- 使用虚幻引擎中的C++导论(二-UE4基类)
使用虚幻引擎中的C++导论(二) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...
- web项目启动报错Unknown character set: 'utf8mb4' in mysql
网上一查,有的说是mysql驱动的问题,有的说创建数据库的时候指定utf8编码,换了各种mysql版本,最后换了5.1.6版本的mysql驱动后成功启动!问题解决!OMG
- tz2txt的安装与使用
tz2txt是一个开源的小工具,用于把帖子的楼主发言保存为txt文件. 目前支持天涯社区.新浪论坛(大部分版块).百度贴吧. 本文介绍tz2txt的安装与使用. 本文目录: 一.下载.安装 二.使用t ...
- IconFont字体制作
1. 第一步.准备svg格式图片 2. 登陆http://iconfont.cn/网站,上传图标. 3. 选中需要制作成iconfont的图标. 4. 将选中的图标转储为项目 5. 下载至本地. 6. ...