您的位置:首页 > 其它

浙大2013复试:PAT 1057. Stack (30)

2014-03-20 20:18 302 查看
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


本题的时间限制比较严格,求中位数的操作很容易超时。这里采用树状数组的解法。

代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<string>
using namespace std;
struct BinTree
{
static const int MAXN = 100010;
vector<int> a;

BinTree()
{
a = vector<int>(MAXN,0);
}

int lowbit(int t)
{
return t&(-t);
}

void update(int t, int d)
{
while (t <= MAXN)
{
a[t] += d;
t += lowbit(t);
}
}

int getsum(int t)
{
int sum(0);
while (t > 0)
{
sum += a[t];
t -= lowbit(t);
}
return sum;
}
int find(int val, int l = 0, int h = MAXN - 1)
{
if (l == h)
return l;
int mid = (l + h) / 2;
if (getsum(mid) < val)
{
//return find(val, mid, h);
return find(val, mid + 1, h);
}
else
{
return find(val, l, mid);
}
}
};
BinTree tree;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
int N(0);
scanf("%d",&N);
char str[20];
int n(0);
stack<int> stk;
while (N--)
{
scanf("%s", &str);
switch (str[1])
{
case 'o':
if (stk.empty())
{
printf("Invalid\n");
}
else
{
n = stk.top();
printf("%d\n", n);
stk.pop();
tree.update(n, -1);
}
break;
case 'u':
scanf("%d", &n);
stk.push(n);
tree.update(n, 1);
break;
case 'e':
if (stk.empty())
printf("Invalid\n");
else
{
printf("%d\n", tree.find((stk.size() + 1) / 2));
}
break;
}
}
return 0;
}


下面的代码是自己第一次做的时候写的,几个case出现了超时。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<string>
#include<iomanip>
using namespace std;
vector<int> coll;
vector<int> tmp;
priority_queue<int> pq;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
int n(0);
scanf("%d", &n);
string str;
char ch[20];
int t(0);
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
if (ch[1] == 'o')
{
if (coll.empty())
{
printf("%s\n", "Invalid");
continue;
}
else
{
printf("%d\n", coll[coll.size() - 1]);
coll.pop_back();
}
}
else if (ch[1] == 'u')
{
scanf("%d", &t);
coll.push_back(t);
}
else if (ch[1] == 'e')
{
while (!pq.empty())
pq.pop();
if (coll.empty())
{
printf("%s\n", "Invalid");
continue;
}
else if (coll.size()==1)
printf("%d\n", coll[0]);
else if (coll.size()%2!=0)
{

int num = coll.size();
int t = (num + 1) / 2;
for (int k = 0; k < num; k++)
{
if (pq.size() < t)
{
pq.push(coll[k]);
}
else
{
if (coll[k] < pq.top())
{
pq.pop();
pq.push(coll[k]);
}
}
}
printf("%d\n", pq.top());
}
else
{
while (!pq.empty())
pq.pop();
int num = coll.size();
int t = num/ 2;
for (int k = 0; k < num; k++)
{
if (pq.size() <t)
{
pq.push(coll[k]);
}
else
{
if (coll[k] < pq.top())
{
pq.pop();
pq.push(coll[k]);
}
}
}
printf("%d\n", pq.top());
}
}
}
return 0;
}


下面是另外一种做法,思路也很巧妙!

//思路很巧妙!!!
#include <iostream>
#include <stack>
#include <set>
#include<vector>
#include <functional>
using namespace std;
multiset<int> uppers;
stack<int> stk;
multiset<int,greater<int> > lowers;
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
int N;
char ch[80];
int n(0);
while (scanf("%d", &N) != EOF)
{
int mid = 0;
for (int i = 0; i < N; i++)
{
scanf("%s", ch);
switch (ch[1])
{
case('u') :
scanf("%d", &n);
stk.push(n);
if (n>mid)
{
uppers.insert(n);
}
else
{
lowers.insert(n);
}
if (uppers.size() > lowers.size())
{
lowers.insert(*uppers.begin());
uppers.erase(uppers.begin());
}
else if (lowers.size() > uppers.size() + 1)
{
uppers.insert(*lowers.begin());
lowers.erase(lowers.begin());
}
mid = *lowers.begin();
break;
case('o'):
if (stk.empty())
{
printf("%s\n", "Invalid");
}
else
{
int t = stk.top();
stk.pop();
printf("%d\n",t);
if (t > *lowers.begin())
{
uppers.erase(uppers.find(t));
}
else
{
lowers.erase(lowers.find(t));
}
if (stk.empty())
{
mid = 0;
}
else
{
if (uppers.size() > lowers.size())
{
lowers.insert(*uppers.begin());
uppers.erase(uppers.begin());
}
else if (lowers.size() > uppers.size() + 1)
{
uppers.insert(*lowers.begin());
lowers.erase(lowers.begin());
}
mid = *lowers.begin();
}
}
break;
case('e'):
if (stk.empty())
{
printf("%s\n", "Invalid");
}
else
{
printf("%d\n", mid);
}
break;
}

}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: