暑假练习赛 004 E Joint Stacks(优先队列模拟)

Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 5818 uDebug

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

Description

A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
A mergeable stack is a stack with "merge" operation. There are three kinds of operation as follows:

- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B

After an operation "merge A B", stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their "push" operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.

Input

There are multiple test cases. For each case, the first line contains an integer , indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.

Output

For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.

Sample Input

4
push A 1
push A 2
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge A B
pop A
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge B A
pop B
pop B
pop B 
0

Sample Output

Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
/*今晚心事繁多,被一个E题卡了很久,心不在焉吧*/
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <queue> #define N 50010 using namespace std; struct node { int num; int id; bool operator < (const node &other) const { return id<other.id; } }; priority_queue<node>a; priority_queue<node>b; priority_queue<node>c; /* 这里一定要用三个优先队列,因为当重复进行merge B A merge A B操作的时候一定会超时的(数据量太大了) 用三个优先队列的话,就能完美解决这个问题了,只要是合并的就合并到c中不管怎么进行操作,都不会再排序了 */ int n; char str[10];//命令 int main() { //freopen("in.txt","r",stdin); int p=1; while(~scanf("%d",&n)!=EOF&&n) { while(!a.empty()) a.pop(); while(!b.empty()) b.pop(); while(!c.empty()) c.pop(); printf("Case #%d: ",p++); //memset(a,0,sizeof a); //memset(b,0,sizeof b); for(int i=0;i<n;i++) { scanf("%s",&str);//输入命令 //cout<<"str="<<str<<endl; char ch; int sh; if(strcmp(str,"push")==0) { //cout<<"第一个命令"<<endl; scanf("%*c%c %d",&ch,&sh); getchar(); //cout<<ch<<" "<<sh<<endl; if(ch=='A') { node fr; fr.num=sh; fr.id=i; a.push(fr); } else if(ch=='B') { node fr; fr.num=sh; fr.id=i; b.push(fr); } //cout<<str<<" "<<ch<<" "<<sh<<endl; } else if(strcmp(str,"pop")==0) { scanf("%*c%c",&ch); getchar(); if(ch=='A') { if(a.empty()) { cout<<c.top().num<<endl; c.pop(); } else { cout<<a.top().num<<endl; a.pop(); } } else if(ch=='B') { if(b.empty()) { cout<<c.top().num<<endl; c.pop(); } else { cout<<b.top().num<<endl; b.pop(); } } //cout<<str<<" "<<ch<<endl; } else if(strcmp(str,"merge")==0) { char s1,s2; scanf("%*c%c %c",&s1,&s2); getchar(); while(!a.empty()) { node fr=a.top(); a.pop(); c.push(fr); } while(!b.empty()) { node fr=b.top(); b.pop(); c.push(fr); } //cout<<str<<" "<<s1<<" "<<s2<<endl; } } } return 0; }
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5762596.html