Chat

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 571    Accepted Submission(s): 136

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 109, and JZP has priority 108 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 109.
 
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: 2
Hint
This problem description does not relate to any real person in THU.
 
Source
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5081 5080 

pid=5079" target="_blank">5079 5078 

pid=5077" target="_blank">5077 

题意:
就是要你写一个聊天窗体管理系统。支持添加窗体。

关闭窗体。管理优先级。。

。。。具体的还是自己读吧。

思路:
认为splay可做就用splay做的。功能也是splay最主要的功能。这题须要注意的是。最后Bye的时候是先给置顶的人然后按队列顺序Bye。
具体见代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=100100;
const int INF=0x3f3f3f3f;
//ll sum[maxn<<1];//子结点的和
map<int,int> mp;
int sz[maxn<<1],pre[maxn<<1],mv[maxn<<1],prr[maxn<<1],ch[maxn<<1][2];//sz记录子树规模,pre记录父结点,val存结点值。 ch[k][0],ch[k][1]k的左右儿子。 int val[maxn<<1];
int mi,mo,root,s[maxn<<1];//mi回收内存。mo分配内存。 root为根。s为内存池
void Treaval(int x)//Debug部分打出来非常清楚
{
if(x)
{
Treaval(ch[x][0]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d \n",x,ch[x][0],ch[x][1],pre[x],sz[x],val[x]);
Treaval(ch[x][1]);
}
}
void Debug()
{
printf("root:%d\n",root);
Treaval(root);
}
void Newnode(int &rt,int v,int pr,int fa)
{
if(mi)//注意mi记录可用空间大小但里面没有空间所以要先减减。不然会出错
rt=s[--mi];
else
rt=mo++;
ch[rt][0]=ch[rt][1]=0;
//sum[r]=k;
pre[rt]=fa;
prr[rt]=pr;
sz[rt]=1;
mv[rt]=rt;
val[rt]=v;
}
void PushUp(int rt)
{
int ls,rs;
if(rt==0)
return;
ls=ch[rt][0],rs=ch[rt][1];
sz[rt]=sz[ls]+sz[rs]+1;
if(prr[mv[ls]]>prr[mv[rs]])
mv[rt]=mv[ls];
else
mv[rt]=mv[rs];
if(prr[rt]>prr[mv[rt]])
mv[rt]=rt;
//sum[rt]=sum[ls]+sum[rs]+val[rt];
}
void Init()//初始化非常重要!
{
mi=root=0;
mo=1;
ch[0][0]=ch[0][1]=pre[0]=sz[0]=mv[0]=prr[0]=0;
val[0]=0;//建一个虚拟根节点。做标记用。 pre==0说明就到根了
Newnode(root,0,0,0);//建真正的根和右儿子。必须加这两个虚拟结点。这样区间操作起来才方便
Newnode(ch[root][1],0,0,root);
PushUp(ch[root][1]);
PushUp(root);
}
void Rotate(int x,int w)//旋转。w为旋转方式。 0为ZAG(x在右边)。1为ZIG(x在左边)。 结点为左子树就右旋。 右子树就左旋
{
int y=pre[x];
ch[y][!w]=ch[x][w];//把x往上转
pre[ch[x][w]]=y;
//PushDown(y)
//PushDown(x)
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][w]=y;
pre[y]=x;
PushUp(y);
}
void Splay(int rt,int goal)//goal为目标父结点
{
int y,w;
while(pre[rt]!=goal)
{
if(pre[pre[rt]]==goal)
Rotate(rt,ch[pre[rt]][0]==rt);
else
{
y=pre[rt];
w=(ch[pre[y]][0]==y);
if(ch[y][w]==rt)
{
Rotate(rt,!w);
Rotate(rt,w);
}
else
{
Rotate(y,w);
Rotate(rt,w);
}
}
}
PushUp(rt);
if(goal==0)//旋转到根时更换根结点
root=rt;
}
int Get_kth(int rt,int k)//取第k个数的标号
{
int tp=sz[ch[rt][0]]+1;
if(tp==k)
return rt;
if(tp>k)
return Get_kth(ch[rt][0],k);
else
return Get_kth(ch[rt][1],k-tp);
}
int Insert(int pos,int v,int pr)//在pos位置插入一个数
{
Splay(Get_kth(root,pos),0);//由于前面有一个虚拟结点所以实际插的位置为pos+1
Splay(Get_kth(root,pos+1),root);
Newnode(ch[ch[root][1]][0],v,pr,ch[root][1]);
PushUp(ch[root][1]);
PushUp(root);
return ch[ch[root][1]][0];
}
void Delete(int rt)//删除结点
{
Splay(rt,0);//先把该点旋转到根
int pos=sz[ch[rt][0]];//获取它前面有多少个数
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+2),root);
s[mi++]=rt;
ch[ch[root][1]][0]=0;
PushUp(ch[root][1]);
PushUp(root);
}
int main()
{
int n,op,i,t,pos,rt,top;
char cmd[15]; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
Init();
mp.clear();
top=-1;
for(i=1;i<=n;i++)
{
scanf("%s",cmd);
if(!strcmp(cmd,"Add"))
{
scanf("%d",&op);
if(mp.count(op))
printf("Operation #%d: same priority.\n",i);
else
{
pos=mp.size();
rt=Insert(pos+1,0,op);
mp[op]=rt;
printf("Operation #%d: success.\n",i);
}
}
else if(!strcmp(cmd,"Close"))
{
scanf("%d",&op);
if(mp.count(op))
{
printf("Operation #%d: close %d with %d.\n",i,op,val[mp[op]]);
Delete(mp[op]);
if(top==mp[op])
top=-1;
mp.erase(op);
}
else
printf("Operation #%d: invalid priority.\n",i);
}
else if(!strcmp(cmd,"Chat"))
{
scanf("%d",&op);
if(top!=-1)
{
val[top]+=op;
printf("Operation #%d: success.\n",i);
}
else if(mp.size())
{
pos=Get_kth(root,2);
val[pos]+=op;
printf("Operation #%d: success.\n",i);
}
else
printf("Operation #%d: empty.\n",i);
}
else if(!strcmp(cmd,"Rotate"))
{
scanf("%d",&op);
if(mp.size()>=op)
{
pos=Get_kth(root,op+1);
Delete(pos);
rt=Insert(1,val[pos],prr[pos]);
mp[prr[pos]]=rt;
if(pos==top)
top=rt;
printf("Operation #%d: success.\n",i);
}
else
printf("Operation #%d: out of range.\n",i);
}
else if(!strcmp(cmd,"Prior"))
{
if(mp.size())
{
pos=mv[root];
Delete(pos);
rt=Insert(1,val[pos],prr[pos]);
mp[prr[pos]]=rt;
if(top==pos)
top=rt;
printf("Operation #%d: success.\n",i);
}
else
printf("Operation #%d: empty.\n",i);
}
else if(!strcmp(cmd,"Choose"))
{
scanf("%d",&op);
if(mp.count(op))
{
pos=mp[op];
Delete(pos);
rt=Insert(1,val[pos],prr[pos]);
if(top==pos)
top=rt;
mp[op]=rt;
printf("Operation #%d: success.\n",i);
}
else
printf("Operation #%d: invalid priority.\n",i);
}
else if(!strcmp(cmd,"Top"))
{
scanf("%d",&op);
if(mp.count(op))
{
top=mp[op];
printf("Operation #%d: success.\n",i);
}
else
printf("Operation #%d: invalid priority.\n",i);
}
else if(!strcmp(cmd,"Untop"))
{
if(top!=-1)
{
top=-1;
printf("Operation #%d: success.\n",i);
}
else
printf("Operation #%d: no such person.\n",i);
}
}
if(top!=-1)
{
if(val[top])
printf("Bye %d: %d\n",prr[top],val[top]);
Delete(top);
}
while(sz[root]>2)
{
pos=Get_kth(root,2);
if(val[pos])
printf("Bye %d: %d\n",prr[pos],val[pos]);
Delete(pos);
}
}
return 0;
}

 

hdu 5071 Chat(模拟|Splay)的更多相关文章

  1. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

  2. HDU 5071 Chat(2014鞍山B,模拟)

    http://acm.hdu.edu.cn/showproblem.php?pid=5071 Chat Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. HDU - 5071 Chat(模拟)

    原题链接 题意:有各种操作,模拟这个程序并输出每次操作的信息 分析:恶心模拟题...用个map记录一下各个等级女孩的谈话数,同时也便于查找权值为u的在不在队列里.因为n很小,其他就暴力模拟了. #in ...

  4. HDU 5071 Chat(2014鞍山赛区现场赛B题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 解题报告:一个管理聊天窗口的程序,一共有八种操作,然后要注意的就是Top操作只是把编号为u的窗口 ...

  5. HDOJ 5071 Chat 模拟

    大模拟: 1>saygoodbye要先对 always on top 的人说 2>对没有说过话的不要说good bye 3>用long long Chat Time Limit: 2 ...

  6. HDU 5071 Chat

    题意: CLJ找了很多妹子-  (题目好没节操-)  对于CLJ和妹子的聊天对话框  有一下几种操作: add  加一个妹子在聊天窗队列末尾  假设这个妹子已经在队列中则add失败 close  关掉 ...

  7. hdu 5071 Chat-----2014acm亚洲区域赛鞍山 B题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 Chat Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. hdu 5071(2014鞍山现场赛B题,大模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...

  9. hdu 5071 vector操作恶心模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=5071 对于每一个窗口,有两个属性:优先级+说过的单词数,支持8个操作:新建窗口,关闭窗口并输出信息,聊天(置顶窗 ...

随机推荐

  1. OC教程10-NSNumber具体

    NSNumber简单介绍 NSNumber是数字的对象形式,由于在OC的数组和字典中仅仅同意存放对象,所以我们有时候须要转化 我们普通的类型是   123 那么 NSNumber类型的是  @123, ...

  2. IOS 原生解析JSON 问题

    服务器----WebService 返回的是JSON数据 IOS解析报错: Error Domain=NSCocoaErrorDomain Code=3840 "Unable to conv ...

  3. openssl AES加密算法API的使用示例

    openssl为用户提供了丰富的指令,同时也提供了供编程调用的API,本文以使用128位aes算法的ecb模式进行加密和解密验证,如下所示 第一种方法,直接使用aes算法提供的api进行调用,代码如下 ...

  4. Mysql存储引擎概念特点介绍及不同业务场景选用依据

    目录 MySQL引擎概述 1 MySAM引擎介绍 2 什么是InnoDB引擎? 3 生产环境中如何批量更改MySQL引擎 4 有关MySQL引擎常见企业面试题 MySQL引擎概述 Mysql表存储结构 ...

  5. (转)IIS5.1的安装配置并发布ASP.NET网站

    最近跟老师做一个桥梁养护系统的项目,要求用VS2008+Sql Server2000,服务器用IIS.由于之前做过的ASP.NET项目都是用的VS内置的服务器,并没有使用过IIS,第一次搭,花了几个小 ...

  6. 浅谈Windows Server APPFABRIC

    hi,everyone !真的是好久好久没有update blog了,因为最近忙着备考,没有时间对<数据结构与算法>进行研究学习了.所以,blog一直未更新.today is Friday ...

  7. n!mod p的求法

    我们假设p为素数,n!=a*pe,则我们需要求解a mod p和e. e是n!能够迭代整除p的次数,因此可以使用下面式子计算: n/p+n/p2+n/p3…… 我们只需要对pt≤n的t进行计算所以复杂 ...

  8. Introduction to the POM

    原文:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html Introduction to the POM ...

  9. js给div动态添加控件,然后给这个控件动态添加事件

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  10. c# 高效分页只需一个dll实例

    第一.首先下载WebUIControl.dll http://pan.baidu.com/s/1gdkilDh 第二.添加引用 三.应用实例-前台代码 <%@ Register Assembly ...