多校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 =100010;
12 #define clc(a,b) memset(a,b,sizeof(a))
13 const double eps = 1e-7;
14 void fre() {freopen("in.txt","r",stdin);}
15 void freout() {freopen("out.txt","w",stdout);}
16 inline int read() {int x=0,f=1;char ch=getchar();while(ch>‘9‘||ch<‘0‘) {if(ch==‘-‘) f=-1;ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();}return x*f;}
17 int n;
18 int sta[3][N],top[3];
19 char op[10],s[5],s1[5];
20 int x[N];
21 void fun(){
22     top[0]=top[1]=top[2]=0;
23     for(int i=0;i<n;i++){
24         scanf("%s%s",op,s);
25         int a=s[0]-‘A‘;
26         if(op[1]==‘u‘){
27            scanf("%d",&x[i]);
28            sta[a][top[a]++]=i;
29         }
30         else if(op[1]==‘o‘){
31             if(!top[a]) a=2;
32             printf("%d\n",x[sta[a][--top[a]]]);
33         }
34         else {
35             scanf("%s",s1);
36             top[2]=merge(sta[0],sta[0]+top[0],sta[1],sta[1]+top[1],sta[2]+top[2])-sta[2];
37             top[0]=top[1]=0;
38         }
39     }
40 }
41 int main(){
42     int cas=1;
43     while(~scanf("%d",&n),n){
44         printf("Case #%d:\n", cas++);
45         fun();
46     }
47     return 0;
48 }
时间: 2024-11-08 21:52:34

多校7 HDU5818 Joint Stacks的相关文章

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

[HDU5818]Joint Stacks

题意:维护两个栈,支持按插入时间合并 本来是左偏树的题,现在用treap水一下 把加入时间作为节点编号就可以很方便地实现了 #include<stdio.h> #include<stdlib.h> int l[100010],r[100010],fix[100010]; unsigned v[100010]; struct pair{ int l,r; pair(int a=0,int b=0){l=a;r=b;} }; pair split(int x,int d){ if(x=

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

【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;

暑假练习赛 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 5818 Joint Stacks

搞了第三个栈来表示合并之后的.偷懒写了一个优先队列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #inc

hdu 5818 (优先队列) Joint Stacks

题目:这里 题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个 栈里的元素全部压进前面的那个栈里面,并且压入后前面的栈的所有元素按其进栈顺序排列然后后面的栈变为了空. push和pop操作都还好,就是优先队列或者栈的操作,主要是merge操作,如果啊按照题目来模拟每次将后面的栈元素取出放入前面的栈,可能会超时,而超时的主要因素是每次都将 元素多的栈压入了元素少