HDU 5071 Chat

  这题没什么好说的:只要维护几个值就好了,因为属性是唯一而且不会改变的所以用Map 来映射下就好了,留下代码备用

  1 #include <cstdio>
  2 #include <cmath>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <cmath>
  6 #include <map>
  7 #include <set>
  8 #include <cstring>
  9 using namespace std;
 10 typedef long long LL;
 11 map<int,int>Hash;
 12 set<int>SET;
 13 map<int,int>::iterator it;
 14 set<int>::iterator it_set;
 15 struct info
 16 {
 17     int w;
 18     LL t;
 19     info(int w,LL t):w(w),t(t){}
 20     info(){t=0;}
 21 };
 22 info que[50050];
 23 int pos,top;
 24 void inint()
 25 {
 26     Hash.clear();
 27     SET.clear();
 28     top=-1;
 29     pos=0;
 30 }
 31 void Rotate(int p,bool flag)
 32 {
 33     if(p<1||p>pos){ printf("out of range");return;}
 34     if(flag)printf("success");
 35     info t=que[p];
 36     for(int i=p;i>=2;i--)
 37     {
 38         que[i]=que[i-1];
 39         it=Hash.find(que[i].w);
 40         Hash.erase(it);
 41         Hash[que[i].w]=i;
 42     }
 43     que[1]=t;
 44     it=Hash.find(que[1].w);
 45     Hash.erase(it);
 46     Hash[que[1].w]=1;
 47 }
 48 void Choose(int p)
 49 {
 50     int t=Hash[p];
 51     if(t==0){printf("invalid priority");return;}
 52     printf("success");
 53     Rotate(Hash[p],false);
 54 }
 55 
 56 void Prior()
 57 {
 58     if(pos==0){printf("empty");return;}
 59     printf("success");
 60     it_set=SET.end();
 61     it_set--;
 62     int num=*it_set;
 63     Rotate(Hash[num],false);
 64 }
 65 void Add(int p)
 66 {
 67     if(Hash[p]!=0){printf("same priority");return;}
 68     printf("success");
 69     que[++pos]=info(p,0);
 70     Hash[p]=pos;
 71     SET.insert(p);
 72 }
 73 void Close(int p)
 74 {
 75     if(Hash[p]==0){printf("invalid priority");return;}
 76     int t=Hash[p];
 77     if(que[t].w!=p) while(1){}
 78     printf("close %d with %d",p,que[t].t);
 79     if(top==p){while(1){}  top=-1;}
 80     it=Hash.find(p);
 81     Hash.erase(it);
 82     SET.erase(p);
 83     for(int i=t;i<pos;i++)
 84     {
 85         que[i]=que[i+1];
 86         it=Hash.find(que[i].w);
 87         Hash.erase(it);
 88         Hash[que[i].w]=i;
 89     }
 90     pos--;
 91 }
 92 void Chat(int p)
 93 {
 94     if(pos==0){printf("empty");return;}
 95     printf("success");
 96     if(top==-1)que[1].t+=p;
 97     else
 98     {
 99         int tmp=Hash[top];
100         que[tmp].t+=p;
101     }
102 }
103 void Top(int p)
104 {
105     if(Hash[p]==0){printf("invalid priority");return;}
106     printf("success");
107     top=p;
108 }
109 void Untop()
110 {
111     if(top!=-1)   {printf("success");top=-1;return;}
112     printf("no such person");
113 }
114 int main()
115 {
116     int t;
117     scanf("%d",&t);
118     char op[10];
119     int a,b,c;
120     while(t--)
121     {
122         int ca=1;
123         inint();
124         int n;
125         scanf("%d",&n);
126         for(int i=1;i<=n;i++)
127         {
128             bool flag=true;
129             printf("Operation #%d: ",ca++);
130             scanf("%s",op);
131             if(strcmp(op,"Untop")==0) {Untop();flag=false;}
132             if(strcmp(op,"Prior")==0) {Prior();flag=false;}
133             if(flag)scanf("%d",&c);
134             if(strcmp(op,"Add")==0) Add(c);
135             if(strcmp(op,"Close")==0) Close(c);
136             if(strcmp(op,"Chat")==0) Chat(c);
137             if(strcmp(op,"Rotate")==0) Rotate(c,true);
138             if(strcmp(op,"Choose")==0) Choose(c);
139             if(strcmp(op,"Top")==0) Top(c);
140             puts(".");
141         }
142         int tt=Hash[top];
143         if(top!=-1)
144         {
145             if(tt!=0&&que[tt].t!=0)
146                 printf("Bye %d: %I64d\n",que[tt].w,que[tt].t);
147         }
148         for(int i=1;i<=pos;i++)
149         {
150            if(i==tt) continue;
151            if(que[i].t==0) continue;
152            printf("Bye %d: %I64d\n",que[i].w,que[i].t);
153         }
154     }
155     return 0;
156 }

 

posted @ 2014-10-22 18:06  默默无语敲代码  阅读(365)  评论(0编辑  收藏  举报