PAT1057. Stack

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 105). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

where key is a positive integer no more than 105.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print "Invalid" instead.

Sample Input:

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Sample Output:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid
思路:有点类似于操作系统的分块查找功能。建立bucket将所有数字分成大组,先大组然后再查询大组里面的元素。这个方法节省了很多时间。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 const int part=317;
 8 const int MAX=100010;
 9 stack<int>st;
10 int bucket[part]={
11     0
12 };
13 int table[MAX]={
14     0
15 };
16 void Peekmedian(int size)
17 {
18     //求第K个
19     if(size%2==0)
20        size/=2;
21     else
22        size=(size+1)/2;
23     int sum=0;
24     int i=0;
25     for(i=0;i<part;i++)
26     {
27         if(sum+bucket[i]<size)
28         {
29             sum+=bucket[i];
30         }
31         else
32         {
33             break;
34         }
35     }
36     for(int j=i*part;j<(i+1)*part;j++)
37     {
38         if(sum+table[j]>=size)
39         {
40             printf("%d\n",j);
41             return ;
42         }
43         else
44         {
45             sum+=table[j];
46         }
47     }
48
49 }
50 int main(int argc, char *argv[])
51 {
52     int N;
53     scanf("%d",&N);
54     while(N--)
55     {
56         char str[20];
57         scanf("%s",str);
58         if(strcmp(str,"Push")==0)
59         {
60             int data;
61             scanf("%d",&data);
62             st.push(data);
63             table[data]++;
64             bucket[data/part]++;
65         }
66         else if(strcmp(str,"Pop")==0)
67         {
68             if(st.empty())
69             {
70                 printf("Invalid\n");
71             }
72             else
73             {
74                 int data=st.top();
75                 st.pop();
76                 printf("%d\n",data);
77                 table[data]--;
78                 bucket[data/part]--;
79             }
80         }
81         else
82         {
83             if(st.empty())
84             {
85                 printf("Invalid\n");
86             }
87             else
88             {
89                 Peekmedian(st.size());
90             }
91         }
92     }
93     return 0;
94 }

时间: 2024-08-07 20:56:09

PAT1057. Stack的相关文章

PAT-1057. Stack (30)--树状数组

今天新学了一个知识,叫做线状数组,主要应用领域 1,数据频繁更新 2,求解某一段区间的和 以上产景情况下可以使用线状数组,更新某一个数据和求某一段时间之和时间复杂度都是Log(N) {常规情况是O(1)和O(N)} 线状数组和RMQ差不多,都可以再Log(N)时间复杂度内求解某一段区间的长度,线状数组额实现方式更为简单,更为方便 以下可以当做线段树的模板 主要函数有lowbit给定制定数字二进制下在末尾为0的个数. Update更新某一节点的值. getsum求解某一区间的值 Update是从叶

pat1057. Stack (30)

1057. Stack (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an el

PAT1057 Stack(树状数组+倍增)

目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈的数字\(key\)范围是[1,100000]. 题目分析 前两个操作用\(stack\)就好. 求中位数.暴力做法即使用上优先队列也是稳稳的超时.考虑树状数组. 压栈时,将\(key\)值对应的位置加1.弹栈减1. 求中位数,可以二分求出\(sum[1:p]==(n+1)/2\)最小的\(p\),

51nod1289 stack

1289 大鱼吃小鱼 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右).问足够长的时间之后,能剩下多少条鱼? Input 第1行:1个数N,表示鱼的数量(1 <= N <= 100000). 第2 - N + 1行:每行两个数A[i], 

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu

About LAMP LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here i

Stack的pop和push操作

#include <stack> #include <cstdio> using namespace std; int main(){ stack<int> s; s.push(1); s.push(2); s.push(3); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top());

从头认识java-9.8 栈(Stack)

这一章节我们来讨论一下栈(Stack). 1.特性 先进后出,当一个元素压进栈里面,他就会处于栈的底部,然后,另一个再压进来,盖在原来的元素上面,原来的元素想出去,只有等上面的元素先顶出栈才有机会. 2.方法演示 package com.ray.ch09; import java.util.Arrays; import java.util.Stack; public class Test { public static void main(String[] args) { Stack<Integ

C#中泛型容器Stack&lt;T&gt;的用法,以及借此实现&rdquo;撤销/重做&rdquo;功能

.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T>,双向链表的泛型实现,不支持索引;ISet<T>不允许被复制,他有2个实现,一个是HashSet<T>,不维持集合元素的排序,另一个是SortedSet<T>,支持集合元素的排序;IDictionary<TKey, TValue>是一个字典集合的泛型接口

Improving performance – A full stack problem

Improving performance – A full stack problem March 6, 2015 by ronald 4 Comments Improving the performance of a web system involves knowledge of how the entire technology stack operates and interacts. There are many simple and common tips that can pro