HDU5071 - Chat(STL模拟)

题目描述

略。。。

题解

现场赛的时候真是脑残。。。用splay去写。。写完发现调试不出来。。。然后才发现数据范围才5000。。。不过那时候只有40分钟了。。用数组模拟了速度敲了一发。写完只剩10几分钟了。。。最终也没调试出来。。赛后想了想发现此题用deque真是巨好写。。

代码:

bye是个坑。必须得在队列里并且是说过话的。。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<algorithm>
  6 using namespace std;
  7 typedef long long LL;
  8 #define maxn 1111111
  9 #define MOD 1000000007
 10 deque< pair<int,LL> >arr;
 11 int tp;
 12 void Add(int x)
 13 {
 14     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
 15         if(it->first==x)
 16         {
 17             puts("same priority.");
 18             return;
 19         }
 20     arr.push_back(make_pair(x,0));
 21     puts("success.");
 22 }
 23 void Close(int x)
 24 {
 25     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
 26         if(it->first==x)
 27         {
 28             if(tp==x) tp=0;
 29             printf("close %d with %I64d.\n",x,it->second);
 30             arr.erase(it);
 31             return;
 32         }
 33     puts("invalid priority.");
 34 }
 35 void Chat(int x)
 36 {
 37     if(arr.size()==0)
 38     {
 39         puts("empty.");
 40         return;
 41     }
 42     if(tp)
 43     {
 44         for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
 45             if(it->first==tp)
 46             {
 47                 it->second+=x;
 48                 break;
 49             }
 50     }
 51     else arr.front().second+=x;
 52     puts("success.");
 53 }
 54 void Rotate(int x)
 55 {
 56     if(arr.size()<x||x<1)
 57     {
 58         puts("out of range.");
 59         return;
 60     }
 61     int cnt=1;
 62     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it,++cnt)
 63         if(cnt==x)
 64         {
 65             pair<int,LL>pa=*it;
 66             arr.erase(it);
 67             arr.push_front(pa);
 68             break;
 69         }
 70     puts("success.");
 71 }
 72 void Prior()
 73 {
 74     if(arr.size()==0)
 75     {
 76         puts("empty.");
 77         return;
 78     }
 79     deque< pair<int,LL> >::iterator mx=arr.begin();
 80     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
 81         if(mx->first<it->first) mx=it;
 82     pair<int,LL>pa=*mx;
 83     arr.erase(mx);
 84     arr.push_front(pa);
 85     puts("success.");
 86 }
 87 void Choose(int x)
 88 {
 89     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
 90         if(it->first==x)
 91         {
 92             pair<int,LL>pa=*it;
 93             arr.erase(it);
 94             arr.push_front(pa);
 95             puts("success.");
 96             return;
 97         }
 98     puts("invalid priority.");
 99 }
100 void Top(int x)
101 {
102     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
103         if(it->first==x)
104         {
105             tp=x;
106             puts("success.");
107             return;
108         }
109     puts("invalid priority.");
110 }
111 void Untop()
112 {
113     if(!tp)
114     {
115         puts("no such person.");
116         return;
117     }
118     tp=0;
119     puts("success.");
120 }
121 void Bye()
122 {
123     deque< pair<int,LL> >::iterator fuck=arr.end();
124     if(tp)
125     {
126         for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
127             if(it->first==tp&&it->second)
128             {
129                 printf("Bye %d: %I64d\n",tp,it->second);
130                 arr.erase(it);
131                 break;
132             }
133     }
134     for(deque< pair<int,LL> >::iterator it=arr.begin(); it!=arr.end(); ++it)
135     if(it->second)
136     {
137         printf("Bye %d: %I64d\n",it->first,it->second);
138     }
139 }
140 int main()
141 {
142     int T;
143     scanf("%d",&T);
144     while(T--)
145     {
146         int n;
147         scanf("%d",&n);
148         arr.clear();
149         tp=0;
150         for(int i=1; i<=n; i++)
151         {
152             printf("Operation #%d: ",i);
153             char op[10];
154             int x;
155             scanf("%s",op);
156             if(op[0]=='A')
157             {
158                 scanf("%d",&x);
159                 Add(x);
160             }
161             else if(op[1]=='l')
162             {
163                 scanf("%d",&x);
164                 Close(x);
165             }
166             else if(op[2]=='a')
167             {
168                 scanf("%d",&x);
169                 Chat(x);
170             }
171             else if(op[0]=='R')
172             {
173                 scanf("%d",&x);
174                 Rotate(x);
175             }
176             else if(op[0]=='P') Prior();
177             else if(op[0]=='C')
178             {
179                 scanf("%d",&x);
180                 Choose(x);
181             }
182             else if(op[0]=='T')
183             {
184                 scanf("%d",&x);
185                 Top(x);
186             }
187             else Untop();
188         }
189         Bye();
190     }
191     return 0;
192 }
View Code

 

posted on 2014-11-06 14:28  仗剑奔走天涯  阅读(350)  评论(0编辑  收藏  举报

导航