http://acm.hdu.edu.cn/showproblem.php?pid=5071
Chat Time limit:2000/1000 MS (java/others) Memory limit:262144/262144 K (java/others) Total Submission (s): 702 Accepted Submission (s): 163
Problem Descriptionas Everyone knows, DRD has no girlfriends. But as everyone also knows, DRD's friend ATM's friend CLJ has many potential girlfriends. One evidence is CLJ ' s chatting record.
CLJ chats with many girls all the time. Sometimes he begins a new conversation and sometimes he ends a conversation. Sometimes he chats with the girl whose window are on the top.
You can imagine CLJ ' s windows as a queue. The first girl in the queue is the top girl if no one are always on top.
Since CLJ is so popular, he begins to assign a unique positive integer as priority for every girl. The higher priority a girl have, the more CLJ likes hers. For example, GYZ have priority 109, and JZP have priority 108 while Sister Soup have priority 1, and face face have priority 2 .
As a famous programmer, CLJ leads a group to implement his own WM (window manager). The WM would log CLJ ' s operations. Now is supposed to implement the log system. The General logging format was "Operation #X: Logmsg.", where X is the number of the operation and logmsg is the logging me Ssage.
There is several kinds of operations CLJ may use:
1.
Add u:CLJ opens a new window whose priority is U, and the new window would be the last window in the window queue. This operation would always be successful except, the only case in which there was already a window with priority U. If It is successful, logmsg would be "success". Otherwise logmsg'll be ' same priority '.
2.
Close u:CLJ closes a window whose priority is U. If there exists such a window, the operation would be successful and logmsg would be "Close u with C", where U is the priori Ty and C is the number of words CLJ have spoken to this window. Otherwise, logmsg'll be ' invalid priority '. Note that any window can be closed.
3.
Chat W:CLJ chats with the top window, and he speaks w words. The top window is the first window of the queue, or the "Always On top" window (as described below) instead if there exist S. If No window is in the queue, logmsg would be ' empty ', otherwise the operation can be successful and logmsg would be "suc Cess ".
4.
Rotate x:CLJ performs one or more alt-tabs to move the x-th window to the first one in the queue. For example, if there is 4 windows in the queue, whose priorities is 1, 3, 5, 7 respectively and CLJ performs "Rotate 3" , then the window's priorities in the queue would become 5, 1, 3, 7. Note if CLJ wants to move the first window to the head, this operation is still considered "successful". If X is out of range (smaller than 1 or larger than the size of the queue), logmsg'll be ' out of range '. Otherwise logmsg should be "success".
5.
Prior:CLJ finds out, the girl with the maximum, and then moves the window to the head of the queue. Note that if the girl and the maximum priority are already the first window, this operation is considered successful as we ll. If the window queue is empty, this operation would fail and logmsg must be "empty". If It is a successful, logmsg must be "success".
6.
Choose u:CLJ chooses the girl with a priority U and moves the window to the head of the queue. This operation are considered successful if and only if the windows with priority U exists. Logmsg for the successful cases should is "success" and for the other cases should is "invalid priority".
7.
Top u:CLJ makes the window of the girl with priority u always on top. Always on top are a special state, which means whoever the first girl in the queue is, the top one must be u if u are always On top. As can see, both girls cannot be always on top at the same time, so if one girl are always on top while CLJ wants anothe R always on top, the first would be is not always on top any more, except the girls is the same one. Anyone can always on top. Logmsg is the same as, that of the Choose operation.
8.
UNTOP:CLJ cancels the ' Always on top ' state of the The girl and the ' Always on top '. That's, the girl who's always on top now isn't in this special state any more. This operation would fail unless there is a girl always on top. If it fails, logmsg should be ' No such person ', otherwise should be ' success '.
As a gentleman, CLJ would say goodbye to every active window he had ever spoken to at last, "active" here means the window Have not been closed so far. The logging format is "Bye u:c" where you are the priority and C are the number of words he have ever spoken to this window. He would always say good bye to the current top girl if he had spoken to her before he closes it.
Inputthe first line contains an integer T (t≤10), denoting the number of the the test cases.
For each test case, the first line contains an integer n (0 < n≤5000), representing the number of operations. Then follow n operations, one in a line. All the parameters is positive integers below 109.
Outputoutput all the logging contents.
Sample Input
118PriorAdd 1Chat 1Add 2Chat 2Top 2Chat 3UntopChat 4Choose 2Chat 5Rotate 2Chat 4Close 2Add 3PriorChat 2Close 1
Sample Output
Operation #1: Empty. Operation #2: Success. Operation #3: Success. Operation #4: Success. Operation #5: Success. Operation #6: Success. Operation #7: Success. Operation #8: Success. Operation #9: Success. Operation #10: Success. Operation #11: Success. Operation #12: Success. Operation #13: Success. Operation #14: Close 2 with 8.Operation #15: success. Operation #16: Success. Operation #17: Success. Operation #18: Close 1 with 11.Bye 3:2HintThis problem description does no relate to any real person in THU.
Source2014 Asia Anshan Regional Contest
In fact, these days really do not want to face this problem. Weak into the tease ratio. It's really sad. The live game did not have this problem completely because oneself is too weak, is an SB simulation problem, then wanted to optimize the constant. The result is a dead loop. It seems that Li-Jie is right, I am still sometimes naive.
。
Test instructions
Read it yourself.
Analysis:
Simulates directly with an array O (n^2). Be able to use red black tree optimization constants, live hand-written bidirectional chain SB has the result of the dead loop. It is important to note that the last bye should first consider top. Then is to pay attention to the spelling of the word, do not confuse the subscript, when knocking not SB can.
/* * Author:fcbruce <[email protected]> * * Time:sat 09:30:24 AM CST */#include <cstdio& GT, #include <iostream> #include <sstream> #include <cstdlib> #include <algorithm> #include < ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include < stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define SQR (x) ((x) * (x)) #define LL long long#define ITN int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#defi NE eps 1e-10#ifdef _win32 #define LLD "%i64d" #else #define LLD "%lld" #endif # define MAXM #define MAXN 8964using namespace std;struct __chat{int u; Long long C;} Chat[maxn];int top,cnt;map<int,int> map;void Add () {int u; scanf ("%d", &u); if (Map.count (u)!=0) {puts ("same priority."); return; } map[u]=cnt; Chat[cnt].u=u; chat[cnt].c=0; cnt++; Puts ("success."); void Close () {int u;scanf ("%d", &u); if (Map.count (u) ==0) {puts ("invalid priority."); return; } if (Top==u) top=-1; int The_one=map[u]; printf ("Close%d with" LLD ". \ n", chat[the_one].u,chat[the_one].c); for (int i=the_one;i<cnt-1;i++) {chat[i]=chat[i+1]; Map[chat[i].u]=i; } cnt--; Map.erase (u);} void Chat () {int w; scanf ("%d", &w); if (cnt==0) {puts ("empty."); return; } if (top!=-1) chat[map[top]].c+=w; else chat[0].c+=w; Puts ("success."); void Rotate () {int x; scanf ("%d", &x); if (x<1 | | x>cnt) {puts ("out of range."); return; } __chat Temp=chat[x-1]; for (int i=x-1;i>0;i--) {chat[i]=chat[i-1]; Map[chat[i].u]=i; } chat[0]=temp; map[temp.u]=0; Puts ("success."); void Prior () {if (cnt==0) {puts ("empty."); return; } int the_one=0; for (int i=1;i<cnt;i++) if (chat[the_one].u<chat[i].u) the_one=i; __chat Temp=chat[the_one]; for (int i=the_one;i>0;i--) {chat[i]=chat[i-1]; map[chat[I].u]=i; } chat[0]=temp; map[temp.u]=0; Puts ("success."); void Choose () {int u; scanf ("%d", &u); if (Map.count (u) ==0) {puts ("invalid priority."); return; } int the_one=map[u]; __chat Temp=chat[the_one]; for (int i=the_one;i>0;i--) {chat[i]=chat[i-1]; Map[chat[i].u]=i; } chat[0]=temp; map[temp.u]=0; Puts ("success."); void Top () {int u; scanf ("%d", &u); if (Map.count (u) ==0) {puts ("invalid priority."); return; } top=u; Puts ("success."); void Untop () {if (Top==-1) puts ("No such person."); else {top=-1; Puts ("success."); }}char Op[233];int Main () {#ifdef fcbruce freopen ("/home/fcbruce/code/t", "R", stdin), #endif//fcbruce int t_t; scanf ("%d", &t_t); while (t_t--) {int n; scanf ("%d", &n); Top=-1; cnt=0; Map.clear (); for (int i=0;i<n;i++) {printf ("Operation #%d:", i+1); scanf ("%s", op); if (strcmp (OP, "add") ==0) Add (); else if (strcmp (OP, "close") ==0) Close (); else if (strcmp (OP, "chat") ==0) Chat (); else if (strcmp (OP, "Rotate") ==0) Rotate (); else if (strcmp (OP, "Prior") ==0) Prior (); else if (strcmp (OP, "Choose") ==0) Choose (); else if (strcmp (OP, "top") ==0) Top (); else Untop (); } if (Top!=-1) if (chat[map[top]].c!=0) printf ("Bye%d:" LLD "\ n", top,chat[map[top]].c); for (int i=0;i<cnt;i++) if (chat[i].u!=top && chat[i].c>0) printf ("Bye%d:" LLD "\ n", Chat[i].u,ch AT[I].C); } return 0;}
HDU 5071 Chat (2014 Anshan B, analogue)