【HDU 5818多校】Joint Stacks

用两个栈模拟,并保存每个点的时间戳。每次合并的时候记录合并时的时间戳mcnt和此时的topa和topb记做ta、tb。

每次pop的时候,如果栈的top的时间戳大于mcnt,则普通地pop,否则就在两个栈ta和tb下面找时间戳最大且还没pop掉的。然后用bj[时间戳]来标记已经pop了。

#include <cstdio>
#include <cstring>
#define N 100005
using namespace std;
struct node{
	int id,v;
}a[N],b[N];
int bj[N],n,topa,topb,cnt,ele,ta,tb,mcnt,cas;
char op[50],st;
void popc(){
	while(bj[a[ta].id])ta--;
	while(bj[b[tb].id])tb--;
	if(a[ta].id>b[tb].id){
		printf("%d\n",a[ta].v);
		bj[a[ta].id]=1;
	}else {
		printf("%d\n",b[tb].v);
		bj[b[tb].id]=1;
	}
}
int main() {
	while(~scanf("%d",&n)&&n){
		printf("Case #%d:\n",++cas);
		topa=topb=cnt=mcnt=0;
		memset(bj,0,sizeof bj);
		for(int i=1;i<=n;i++){
			scanf("%s %c",op,&st);
			if(op[1]==‘u‘){
				scanf("%d",&ele);
				if(st==‘A‘)
					a[++topa]=(node){++cnt,ele};
				else
					b[++topb]=(node){++cnt,ele};
			}else if(op[1]==‘o‘){
				if(st==‘A‘){
					if(a[topa].id>mcnt){
						printf("%d\n",a[topa].v);
						bj[a[topa].id]=1;
						topa--;
					}
					else
						popc();
				}else{
					if(b[topb].id>mcnt){
						printf("%d\n",b[topb].v);
						bj[b[topb].id]=1;
						topb--;
					}
					else
						popc();
				}
			}else if(op[1]==‘e‘){
				scanf(" %c",&st);
				mcnt=cnt;
				ta=topa;
				tb=topb;
			}
		}
	}
	return 0;
}

wa了好几发,结果原因是 “if(a[topa].id>mcnt)”写的是>=,为什么等于不可以呢,因为popc函数里没有top--,这样下一次top的值可能已经pop掉了。那如果在popc里面top--呢,也不行,因为pop栈a的ta时,可能在pop栈b,这样to pa--的话就错了。这题用优先队列也很方便。

时间: 2024-12-15 17:15:59

【HDU 5818多校】Joint Stacks的相关文章

HDU 5818 Joint Stacks(联合栈)

HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of

HDU 5818:Joint Stacks(stack + deque)

http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks 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

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 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 wor

多校7 HDU5818 Joint Stacks

1 多校7 HDU5818 Joint Stacks 2 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 3 思路:开三个栈,并且用到了merge函数 4 O(n)的复杂度 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define LL long long 9 const int inf = 0x3f3f3f3f; 10 const int MOD =998244353; 11 const int N =

暑假练习赛 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 d

hdu 4893 (多校1007)Wow! Such Sequence!(线段树&amp;二分&amp;思维)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 352    Accepted Submission(s): 104 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

hdu-5818 Joint Stacks(模拟)

题目链接: Joint Stacks Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) 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

hdu 5344 (多校联赛) MZL&#39;s xor --- 位运算

here:    首先看一下题吧:题意就是让你把一个序列里所有的(Ai+Aj) 的异或求出来.(1<=i,j<=n) Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all (Ai+Aj)(1≤i,j≤n) The xor of an array B is defined as B1 xor B2...xor B

HDU 4915 多校5 Parenthese sequence

比赛的时候想了一个自认为对的方法,WA到死,然后还一直敲下去,一直到晚上才想到反例 找是否存在解比较好找,这种左右括号序列,把(当成1,把)当成-1,然后从前往后扫,+1或者-1 遇到?就当初(,然后如果扫到最后 中间没有出现负数说明左括号没问题 然后同样的方法从后往前扫,判断右括号那里是不是有问题即可.都没问题就有解,否则无解 当然应该要先判断下序列长度是不是偶数,奇数肯定是无解 至于为什么要像之前的处理即可判断有无解,首先只有正好走完的时候 和值为0才是真正合法(因为这个时候左右括号都对应了