PAT 1057. Stack (30)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057

用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好

#include <cstdio>
#include <string>
#include <vector>
#include <stack>
using namespace std;

const int NUM=100001;

struct TreeArray
{
vector<int> cStore;
TreeArray()
{
cStore=vector<int>(NUM,0);
}
int lowerBit(int x)
{
return x&(-x);
}
void add(int location,int addedValue)
{
while(location<=NUM)
{
cStore[location]+=addedValue;
location+=lowerBit(location);
}
}
int getSum(int location)
{
int sum(0);
while(location>0)
{
sum+=cStore[location];
location-=lowerBit(location);
}
return sum;
}

int findMedian(int toFind,int low=1,int high=NUM)
{
if(low==high)
return low;
int median=(low+high)>>1;
if(getSum(median)<toFind)
{
return findMedian(toFind,median+1,high);
}
else
{
return findMedian(toFind,low,median);
}
}
};

TreeArray treeArray;
stack<int> s;

int _tmain(int argc, _TCHAR* argv[])
{
int N;
scanf("%d",&N);
char command[20];
int key;
for(int i=0;i<N;++i)
{
scanf("%s",command);
switch(command[1])
{
case ‘u‘:
scanf("%d",&key);
s.push(key);
treeArray.add(key,1);
break;
case ‘e‘:
if(s.empty())
printf("Invalid\n");
else
{
printf("%d\n",treeArray.findMedian((s.size()+1)/2));
}
break;
case ‘o‘:
if(s.empty())
printf("Invalid\n");
else
{
key=s.top();
s.pop();
printf("%d\n",key);
treeArray.add(key,-1);
break;
}
}
}
return 0;
}

PAT 1057. Stack (30)

时间: 2024-11-08 02:16:02

PAT 1057. Stack (30)的相关文章

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

1057 Stack (30 分) 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). No

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

不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekMedian:返回stack中第(n+1)/2个小的数 建立一个栈来模拟push和pop,另外还需要树状数组,来统计栈中<=某个数的总个数不了解树状数组的建议学习一下,很有用的.树状数组为c,有个虚拟的a数组,a[i]表示i出现的次数sum(i)就是统计a[1]~a[i]的和,即1~i出现的次数当我要

PAT (Advanced Level) 1057. Stack (30)

树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace

1057. Stack (30)

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

PAT 1057 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

1057. Stack (30) - 树状数组

题目例如以下: 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

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

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

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和找中位数三种操作. 思路: 好久没写题.感觉傻逼题写多了稍微有点数据结构的都不会写了. pop和push操作就不说了. 找中位数的话就二分去找某一个数前面一共有多少小于他的数,找到那个小于他的数刚好等于一半的. 找的过程中要用到前缀和,所以自然而然就应该上树状数组. 要注意树状数组的界应该是1e5而

PAT 甲级 1057 Stack

https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 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 o