hdu5071 2014 Asia AnShan Regional Contest B Chat

模拟题:

add的时候出现过的则不再添加

close的时候会影响到top

rotate(Prior、Choose)的时候会影响到top

  1 /*===============================================================
  2 *   Copyright (C) 2014 All rights reserved.
  3 *   
  4 *   File Name: hdu5071.cpp
  5 *   Author:sunshine
  6 *   Created Time: 2014-11-11
  7 *
  8 ================================================================*/
  9 #include <map>
 10 #include <queue>
 11 #include <stack>
 12 #include <math.h>
 13 #include <stdio.h>
 14 #include <string.h>
 15 #include <iostream>
 16 #include <algorithm>
 17 
 18 using namespace std;
 19 
 20 #define SUCCESS puts("success.")
 21 #define EMPTY puts("empty.")
 22 #define INVALID puts("invalid priority.") 
 23 #define SAME puts("same priority.")
 24 #define OUT_OF_RANGE puts("out of range.")
 25 #define NO_SUCH_PERSON puts("no such person.")
 26 
 27 const int MAXN = 5005;
 28 
 29 struct Node{
 30     int value;
 31     long long word_num;
 32     void set(int t_value){
 33         value = t_value;
 34         word_num = 0;
 35     }
 36 }node[MAXN];
 37 
 38 int top_u;// -1 is no top, value is the top number 
 39 int que_size;
 40 map<int,int>M;
 41 
 42 void init(){
 43     top_u = -1;
 44     que_size = 0;
 45     M.clear();
 46 }
 47 
 48 int find_u_id(int u){
 49     int u_id = -1;
 50     for(int i = 0;i < que_size;i ++){
 51         if(node[i].value == u){
 52             u_id = i;
 53             break;
 54         }
 55     }
 56     return u_id;
 57 }
 58 
 59 void Rotate(int x){
 60     if(que_size < x){
 61         OUT_OF_RANGE;
 62     }else{
 63         Node tmp = node[x - 1];
 64         for(int i = x - 2;i >= 0;i --){
 65             node[i + 1] = node[i];
 66         }
 67         node[0] = tmp;
 68         SUCCESS;
 69     }
 70 }
 71 
 72 void Prior(){
 73     if(que_size == 0){
 74         EMPTY;
 75     }else{
 76         int max_value = -1, max_value_id = -1;
 77         for(int i = 0;i < que_size;i ++){
 78             if(node[i].value > max_value){
 79                 max_value = node[i].value;
 80                 max_value_id = i;
 81             }
 82         }
 83         Rotate(max_value_id + 1);
 84     }
 85 }
 86 
 87 void Choose(int u){
 88     if(M[u] == 0){
 89         INVALID;
 90     }else{
 91         int u_id = find_u_id(u);
 92         Rotate(u_id + 1);
 93     }
 94 }
 95 
 96 void Top(int u){
 97     if(M[u] == 0){
 98         INVALID;
 99     }else{
100         top_u = u;
101         SUCCESS;
102     }
103 }
104 
105 void Untop(){
106     if(top_u == -1){
107         NO_SUCH_PERSON;
108     }else{
109         top_u = -1;
110         SUCCESS;
111     }
112 }
113 
114 void Add(int u){
115     if(M[u] == 1){
116         SAME;
117     }else{
118         M[u] = 1;
119         node[que_size ++].set(u);
120         SUCCESS;
121     }
122 }
123 
124 void Close(int u){
125     if(M[u] == 0){
126         INVALID;
127     }else{
128         M[u] = 0;
129         int u_id = find_u_id(u);
130         if(u == top_u) top_u = -1;
131         printf("close %d with %I64d.\n", u, node[u_id].word_num);
132         for(int i = u_id;i < que_size;i ++){
133             node[i] = node[i+1];
134         }
135         que_size --;
136     }
137 }
138 
139 void Chat(int w){
140     if(que_size == 0){
141         EMPTY;
142     }else{
143         if(top_u != -1){
144             node[find_u_id(top_u)].word_num += w;
145         }else{
146             node[0].word_num += w;
147         }
148         SUCCESS;
149     }
150 }
151 
152 void Bye(){
153     if(que_size == 0) return ;
154     int top_id = -1;
155     if(top_u != -1){
156         top_id = find_u_id(top_u);
157         if(top_id != -1 && node[top_id].word_num != 0){
158             printf("Bye %d: %I64d\n",node[top_id].value, node[top_id].word_num);
159         }
160     }
161     for(int i = 0;i < que_size;i ++){
162         if(i == top_id) continue;
163         if(node[i].word_num == 0) continue;
164         printf("Bye %d: %I64d\n",node[i].value, node[i].word_num);
165     }    
166 }
167 
168 int main(){
169     int T, n;
170     char command[10];
171     scanf("%d",&T);
172     while(T --){
173         scanf("%d\n",&n);
174         init();
175         for(int i = 1;i <= n;i ++){
176             scanf("%s",command);
177             printf("Operation #%d: ",i);
178             if(0 == strcmp("Prior",command)){
179                 Prior();
180             }else if(0 == strcmp("Untop",command)){
181                 Untop();
182             }else {
183                 int u;
184                 scanf("%d",&u);
185                 if(0 == strcmp("Add",command)){
186                     Add(u);
187                 }else if(0 == strcmp("Close",command)){
188                     Close(u);
189                 }else if(0 == strcmp("Chat",command)){
190                     Chat(u);
191                 }else if(0 == strcmp("Rotate",command)){
192                     Rotate(u);
193                 }else if(0 == strcmp("Choose",command)){
194                     Choose(u);
195                 }else if(0 == strcmp("Top",command)){
196                     Top(u);
197                 }
198             }
199         }
200         Bye();
201     }
202     return 0;
203 }
View Code

 

posted @ 2014-11-12 10:00  _sunshine  阅读(315)  评论(0编辑  收藏  举报