1 #include<stdio.h>
2 #include<string.h>
3 int y[5005];
4 int num[5005];
5 char s[15];
6 int main()
7 {
8 int T,i,j,k,x,u,n,flag,cnt,top,yy,maxx,nn;
9 // freopen("in.txt","r",stdin);
10 //freopen("out.txt","w",stdout);
11 scanf("%d",&T);
12 while (T--)
13 {
14 scanf("%d",&n);
15 cnt=0; top=0;
16 memset(num,0,sizeof(num));
17 memset(y,0,sizeof(y));
18 for (j=1;j<=n;j++)
19 {
20 scanf("%s",s);
21 printf("Operation #%d: ",j);
22 if (strcmp(s,"Chat")==0)
23 {
24 scanf("%d",&x);
25 if (cnt==0) {printf("empty.\n"); continue; }
26 if (top!=0) {num[top]=num[top]+x; printf("success.\n"); continue; }
27 num[1]=num[1]+x; printf("success.\n");
28 }
29 else if (strcmp(s,"Add")==0)
30 {
31 scanf("%d",&u); flag=1;
32 for (i=1;i<=cnt;i++)
33 if (y[i]==u) flag=0;
34 if (flag==0) {printf("same priority.\n"); continue; }
35 printf("success.\n");
36 cnt++; num[cnt]=0; y[cnt]=u;
37 }
38 else if (strcmp(s,"Close")==0)
39 {
40 scanf("%d",&u);flag=1; k=0;
41 for (i=1;i<=cnt;i++)
42 if (y[i]==u) { flag=0; k=i;}
43 if (flag) {printf("invalid priority.\n"); continue; }
44 printf("close %d with %d.\n",u,num[k]);
45 if (top==k) top=0;
46 else if (top>k) top--;
47 cnt--;
48 for (i=k;i<=cnt;i++) {num[i]=num[i+1]; y[i]=y[i+1]; }
49 }
50 else if (strcmp(s,"Rotate")==0)
51 {
52 scanf("%d",&x);
53 if (x<1||x>cnt) {printf("out of range.\n"); continue; }
54 printf("success.\n");
55 if (top==x) top=1;
56 else if (top<x&&top!=0) top++;
57 nn=num[x]; yy=y[x];
58 for (i=x;i>1;i--) {num[i]=num[i-1]; y[i]=y[i-1]; }
59 num[1]=nn; y[1]=yy;
60 }
61 else if (strcmp(s,"Prior")==0)
62 {
63 if (cnt==0) {printf("empty.\n"); continue; }
64 printf("success.\n"); maxx=-1; x=0;
65 for (i=1;i<=cnt;i++)
66 if (y[i]>maxx) {maxx=y[i]; x=i; }
67 if (top==x) top=1;
68 else if (top<x&&top!=0) top++;
69 nn=num[x]; yy=y[x];
70 for (i=x;i>1;i--) {num[i]=num[i-1]; y[i]=y[i-1]; }
71 num[1]=nn; y[1]=yy;
72 }
73 else if (strcmp(s,"Choose")==0)
74 {
75 scanf("%d",&u);flag=1; k=0;
76 for (i=1;i<=cnt;i++)
77 if (y[i]==u) { flag=0; k=i;}
78 if (flag) {printf("invalid priority.\n"); continue; }
79 if (top==k) top=1;
80 else if (top<k&&top!=0) top++;
81 printf("success.\n"); nn=num[k]; yy=y[k];
82 for (i=k;i>1;i--) {num[i]=num[i-1]; y[i]=y[i-1]; }
83 num[1]=nn; y[1]=yy;
84 }
85 else if (strcmp(s,"Top")==0)
86 {
87 scanf("%d",&u);flag=1; k=0;
88 for (i=1;i<=cnt;i++)
89 if (y[i]==u) { flag=0; k=i;}
90 if (flag) {printf("invalid priority.\n"); continue; }
91 top=k; printf("success.\n");
92 }
93 else if (strcmp(s,"Untop")==0)
94 {
95 if (top==0) printf("no such person.\n");
96 else printf("success.\n");
97 top=0;
98 }
99 }
100 if (top!=0&&num[top]!=0) printf("Bye %d: %d\n",y[top],num[top]);
101 for (i=1;i<=cnt;i++)
102 if (i!=top&&num[i]!=0)
103 printf("Bye %d: %d\n",y[i],num[i]);
104 }
105 }
http://acm.hdu.edu.cn/showproblem.php?pid=5071