HDU 5818 Joint Stacks(优先队列+swap)



Problem 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  N(0<N105) , 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
 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
struct Node{
    int num;
    int time;
      Node(int a = 0 , int b = 0){  
        num=a,time=b;  
    }  
};
bool operator<(Node a,Node b){
    return a.time<b.time;
}
 
priority_queue<Node> A;
priority_queue<Node> B;

//struct cmp{
//
//	bool operator()(Node a,Node b){
//	    return a.time<b.time;
//	}
//};
//priority_queue<Node,vector<Node>,cmp> A;
//priority_queue<Node,vector<Node>,cmp> B;
 

void init(){
	while(!A.empty()){
		A.pop();
	}
	while(!B.empty()){
		B.pop();
	}

}
 
char opr[7];
char obj;
char obj2;
int i;
 
int main(){
	
 
    int n;
    int c=1;
    while(~scanf("%d",&n)&&n){
        int t=0;
        init();
        printf("Case #%d:\n",c++);

        while(n--){
 			
			
            scanf("%s %c",opr,&obj);
            if(opr[1]=='u'){
                scanf("%d",&i);
 
                if(obj=='A') A.push(Node(i,++t));
                if(obj=='B') B.push(Node(i,++t));
 
 
            }
 
            else if(opr[1]=='o'){
 
                if(obj=='A'){
 
                    printf("%d\n",A.top().num);
                    A.pop();
                }
 
                if(obj=='B'){
                    printf("%d\n",B.top().num);
                    B.pop();
                }
 
 
            }
 
            else if(opr[1]=='e'){
 
                getchar();
                scanf("%c",&obj2);

				
                if(obj=='A'&&obj2=='B'){
                	if(A.size()<B.size()) swap(A,B);
 
                    while(!B.empty()){
 
                        A.push(B.top());
                        B.pop();

                    }
 
                }
 
                if(obj=='B'&&obj2=='A'){
                	if(B.size()<A.size()) swap(A,B);
 
                    while(!A.empty()){
 
                        B.push(A.top());
                        A.pop();    
                    }
                }
 
            }
 
 
        }
 
    }
}

题意:有A B两个栈,对A B进行push,pop,merge操作,push pop和栈操作中的进栈出栈一样,merge操作是将后一个栈融合到前一个栈中,融合时元素按进栈顺序排列;输出每次pop的元素;

题意给的是栈, 一开始可能会考虑用栈做;但是后面merge的时候不好操作,因为要按push的时间进栈,所以考虑用优先队列进行操作;
用结构体变量存储进栈时的数字和进栈时的时间,自定义优先队列的优先级为时间(时间短的在队末);
进队时用结构体构造一个变量进队,merge时不断让后一个队列的队首进入前一个队列,再把后一个队列的队首pop;
注意的是:譬如merge A B,当前A元素很少而B元素很多时,不断的push pop很容易超时,此时我们考虑将它们队内元素交换,这样可以节省时间;
交换时我们直接用algorithm库的swap函数:swap只交换内部的指针,时间复杂度为O(1);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值