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 Description
As 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 is 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 is “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 has, the more CLJ likes her. For example, GYZ has priority 10 9, and JZP has priority 10 8 while Sister Soup has priority 1, and Face Face has priority 2.
As a famous programmer, CLJ leads a group to implement his own WM(window manager). The WM will log CLJ’s operations. Now you are supposed to implement the log system. The general logging format is “Operation #X: LOGMSG.”, where X is the number of the operation and LOGMSG is the logging message.
There are several kinds of operations CLJ may use:
1. Add u: CLJ opens a new window whose priority is u, and the new window will be the last window in the window queue. This operation will always be successful except the only case in which there is already a window with priority u. If it is successful, LOGMSG will be “success”. Otherwise LOGMSG will be “same priority”.
2. Close u: CLJ closes a window whose priority is u. If there exists such a window, the operation will be successful and LOGMSG will be “close u with c”, where u is the priority and c is the number of words CLJ has spoken to this window. Otherwise, LOGMSG will 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 in the queue, or the “always on top” window (as described below) instead if there exists. If no window is in the queue, LOGMSG will be “empty”, otherwise the operation can be successful and LOGMSG will be “success”.
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 are 4 windows in the queue, whose priorities are 1, 3, 5, 7 respectively and CLJ performs “Rotate 3”, then the window’s priorities in the queue will become 5, 1, 3, 7. Note that 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 will be “out of range”. Otherwise LOGMSG should be “success”.
5. Prior: CLJ finds out the girl with the maximum priority and then moves the window to the head of the queue. Note that if the girl with the maximum priority is already the first window, this operation is considered successful as well. If the window queue is empty, this operation will fail and LOGMSG must be “empty”. If it is successful, LOGMSG must be “success”.
6. Choose u: CLJ chooses the girl with priority u and moves the window to the head of the queue.This operation is considered successful if and only if the window with priority u exists. LOGMSG for the successful cases should be “success” and for the other cases should be “invalid priority”.
7. Top u: CLJ makes the window of the girl with priority u always on top. Always on top is a special state, which means whoever the first girl in the queue is, the top one must be u if u is always on top. As you can see, two girls cannot be always on top at the same time, so if one girl is always on top while CLJ wants another always on top, the first will be not always on top any more, except the two girls are the same one. Anyone can be always on top. LOGMSG is the same as that of the Choose operation.
8. Untop: CLJ cancels the “always on top” state of the girl who is always on top. That is, the girl who is always on top now is not in this special state any more. This operation will fail unless there is one girl always on top. If it fails, LOGMSG should be “no such person”, otherwise should be “success”.
As a gentleman, CLJ will say goodbye to every active window he has ever spoken to at last, “active” here means the window has not been closed so far. The logging format is “Bye u: c” where u is the priority and c is the number of words he has ever spoken to this window. He will always say good bye to the current top girl if he has spoken to her before he closes it.
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 is 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 is “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 has, the more CLJ likes her. For example, GYZ has priority 10 9, and JZP has priority 10 8 while Sister Soup has priority 1, and Face Face has priority 2.
As a famous programmer, CLJ leads a group to implement his own WM(window manager). The WM will log CLJ’s operations. Now you are supposed to implement the log system. The general logging format is “Operation #X: LOGMSG.”, where X is the number of the operation and LOGMSG is the logging message.
There are several kinds of operations CLJ may use:
1. Add u: CLJ opens a new window whose priority is u, and the new window will be the last window in the window queue. This operation will always be successful except the only case in which there is already a window with priority u. If it is successful, LOGMSG will be “success”. Otherwise LOGMSG will be “same priority”.
2. Close u: CLJ closes a window whose priority is u. If there exists such a window, the operation will be successful and LOGMSG will be “close u with c”, where u is the priority and c is the number of words CLJ has spoken to this window. Otherwise, LOGMSG will 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 in the queue, or the “always on top” window (as described below) instead if there exists. If no window is in the queue, LOGMSG will be “empty”, otherwise the operation can be successful and LOGMSG will be “success”.
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 are 4 windows in the queue, whose priorities are 1, 3, 5, 7 respectively and CLJ performs “Rotate 3”, then the window’s priorities in the queue will become 5, 1, 3, 7. Note that 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 will be “out of range”. Otherwise LOGMSG should be “success”.
5. Prior: CLJ finds out the girl with the maximum priority and then moves the window to the head of the queue. Note that if the girl with the maximum priority is already the first window, this operation is considered successful as well. If the window queue is empty, this operation will fail and LOGMSG must be “empty”. If it is successful, LOGMSG must be “success”.
6. Choose u: CLJ chooses the girl with priority u and moves the window to the head of the queue.This operation is considered successful if and only if the window with priority u exists. LOGMSG for the successful cases should be “success” and for the other cases should be “invalid priority”.
7. Top u: CLJ makes the window of the girl with priority u always on top. Always on top is a special state, which means whoever the first girl in the queue is, the top one must be u if u is always on top. As you can see, two girls cannot be always on top at the same time, so if one girl is always on top while CLJ wants another always on top, the first will be not always on top any more, except the two girls are the same one. Anyone can be always on top. LOGMSG is the same as that of the Choose operation.
8. Untop: CLJ cancels the “always on top” state of the girl who is always on top. That is, the girl who is always on top now is not in this special state any more. This operation will fail unless there is one girl always on top. If it fails, LOGMSG should be “no such person”, otherwise should be “success”.
As a gentleman, CLJ will say goodbye to every active window he has ever spoken to at last, “active” here means the window has not been closed so far. The logging format is “Bye u: c” where u is the priority and c is the number of words he has ever spoken to this window. He will always say good bye to the current top girl if he has spoken to her before he closes it.
Input
The first line contains an integer T (T ≤ 10), denoting the number of 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 are positive integers below 10 9.
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 are positive integers below 10 9.
Output
Output all the logging contents.
Sample Input
1 18 Prior Add 1 Chat 1 Add 2 Chat 2 Top 2 Chat 3 Untop Chat 4 Choose 2 Chat 5 Rotate 2 Chat 4 Close 2 Add 3 Prior Chat 2 Close 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 not relate to any real person in THU.
Source
其实这几天真的不想面对这题,弱成逗比,真是令人忧伤。现场赛没出这题完全是因为自己太弱,就是一道SB模拟题,当时就想优化常数,结果优成死循环,看来丽洁说得没错,我果然还是sometimes naive。。。
题意:
自己读。
分析:
直接用数组O(n^2)模拟,可以用红黑树优化常数,现场赛手写双向链SB了结果死循环,要注意的是最后Bye的时候要先考虑Top,然后就是注意单词拼写,不要搞混下标,敲的时候不SB就行。
/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Sat 25 Oct 2014 09:30:24 AM CST
*
*/
#include <cstdio>
#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
#define eps 1e-10
#ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif
#define maxm
#define maxn 8964
using 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,chat[i].c);
}
return 0;
}