http://acm.hdu.edu.cn/showproblem.php?pid=5070

先跳过。

 

http://acm.hdu.edu.cn/showproblem.php?pid=5071

 

维护一个聊天队列,有8种操作,每次操作完要打印对应的信息,认真读题,按题意模拟,一维数组模拟队列就可以,5000操作,交换删除等on暴力复杂度也不会超时。

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<map>
  4 using namespace std;
  5 const int M=32;
  6 char a[M];
  7 char b[M][M]={"","Add","Close","Chat","Rotate","Prior","Choose","Top","Untop"};
  8 map<int,int> mp;
  9 int sta[5010],top,cnt,x;
 10 void success(){
 11     puts("success.");
 12 }
 13 void invalid(){
 14     puts("invalid priority.");
 15 }
 16 void Empty(){
 17     puts("empty.");
 18 }
 19 void solve1(){
 20     for(int i=0;i<cnt;i++){
 21         if(sta[i]==x){
 22             puts("same priority.");
 23             return ;
 24         }
 25     }
 26     sta[cnt++]=x;
 27     success();
 28 }
 29 void solve2(){
 30     for(int i=0;i<cnt;i++){
 31         if(sta[i]==x){
 32             if(top==x) top=-1;
 33             for(int j=i;j<cnt-1;j++){
 34                 sta[j]=sta[j+1];
 35             }
 36             cnt--;
 37             printf("close %d with %d.\n",x,mp[x]);
 38             mp.erase(x);
 39             return ;
 40         }
 41     }
 42     invalid();
 43 }
 44 void solve3(){
 45     if(!cnt){
 46         Empty();
 47         return ;
 48     }
 49     if(top!=-1){
 50         mp[top]+=x;
 51         success();
 52         return ;
 53     }
 54     mp[sta[0]]+=x;
 55     success();
 56 }
 57 void solve4(int id){
 58     if(id<1||id>cnt){
 59         puts("out of range.");
 60         return ;
 61     }
 62     int tmp=sta[id-1];
 63     for(int i=id-1;i>0;i--){
 64         sta[i]=sta[i-1];
 65     }
 66     sta[0]=tmp;
 67     success();
 68 }
 69 void solve5(){
 70     if(!cnt){
 71         Empty();
 72         return ;
 73     }
 74     int id=0;
 75     for(int i=0;i<cnt;i++){
 76         if(sta[i]>sta[id]){
 77             id=i;
 78         }
 79     }
 80     solve4(id+1);
 81 }
 82 void solve6(){
 83     for(int i=0;i<cnt;i++){
 84         if(sta[i]==x){
 85             solve4(i+1);
 86             return ;
 87         }
 88     }
 89     invalid();
 90 }
 91 void solve7(){
 92     for(int i=0;i<cnt;i++){
 93         if(sta[i]==x){
 94             top=x;
 95             success();
 96             return ;
 97         }
 98     }
 99     invalid();
100 }
101 void solve8(){
102     if(top!=-1){
103         top=-1;
104         success();
105         return ;
106     }
107     puts("no such person.");
108 }
109 int main(){
110     int t,n;
111     while(~scanf("%d",&t)){
112         while(t--){
113             scanf("%d",&n);
114             int cas=1;
115             top=-1,cnt=0;
116             mp.clear();
117             while(n--){
118                 scanf("%s",a);
119                 int id=1;
120                 for(int i=1;i<=8;i++){
121                     if(!strcmp(a,b[i])){
122                         id=i;
123                         break;
124                     }
125                 }
126                 if(id!=5&&id!=8){
127                     scanf("%d",&x);
128                 }
129                 printf("Operation #%d: ",cas++);
130                 if(id==1){
131                     solve1();
132                 }
133                 else if(id==2){
134                     solve2();
135                 }
136                 else if(id==3){
137                     solve3();
138                 }
139                 else if(id==4){
140                     solve4(x);
141                 }
142                 else if(id==5){
143                     solve5();
144                 }
145                 else if(id==6){
146                     solve6();
147                 }
148                 else if(id==7){
149                     solve7();
150                 }
151                 else{
152                     solve8();
153                 }
154             }
155             if(top!=-1&&mp[top]){
156                 printf("Bye %d: %d\n",top,mp[top]);
157             }
158             for(int i=0;i<cnt;i++){
159                 if(top!=sta[i]&&mp[sta[i]]){
160                     printf("Bye %d: %d\n",sta[i],mp[sta[i]]);
161                 }
162             }
163         }
164     }
165     return 0;
166 }
View Code

相关文章:

  • 2021-11-09
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-12
猜你喜欢
  • 2022-03-03
  • 2021-09-19
  • 2021-09-11
  • 2021-08-01
  • 2022-12-23
  • 2022-01-14
  • 2021-08-31
相关资源
相似解决方案