HDOJ 5071 Chat 模拟


大模拟:

1》saygoodbye要先对 always on top 的人说
2》对没有说过话的不要说good bye
3》用long long


Chat

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 337    Accepted Submission(s): 82


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
2014 Asia AnShan Regional Contest




#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>using namespace std;typedef long long int LL;const int INF=0x3f3f3f3f;const int HEAD=0;
const int TAIL=6400;struct CHART
{int pro,front,back;LL w;
}chat[6500];int n,x,id;
int alwayson;
set<int> pc;
char cmd[50];void init()
{id=1;pc.clear();alwayson=-1;memset(chat,0,sizeof(chat));chat[HEAD].front=HEAD; chat[HEAD].back=TAIL;chat[TAIL].front=HEAD; chat[TAIL].back=TAIL;chat[HEAD].pro=-INF-1; chat[TAIL].pro=-INF-1;
}void Add(int u)
{if(pc.count(u)){puts("same priority.");return ;}pc.insert(u);chat[id].w=0; chat[id].pro=u;chat[id].front=chat[TAIL].front;chat[id].back=TAIL;chat[chat[TAIL].front].back=id;chat[TAIL].front=id;id++;puts("success.");
}void Close(int u)
{if(pc.count(u)==0){puts("invalid priority.");return ;}pc.erase(u);if(alwayson==u) alwayson=-1;int pos=HEAD;for(pos=HEAD;pos!=TAIL;pos=chat[pos].back){if(chat[pos].pro==u) break;}int bc=chat[pos].back;int ft=chat[pos].front;chat[bc].front=ft;chat[ft].back=bc;printf("close %d with %I64d.\n",chat[pos].pro,chat[pos].w);
}void Chat(int w)
{if(chat[HEAD].back==TAIL){puts("empty.");return ;}puts("success.");int u=-1;if(alwayson!=-1) u=alwayson;else u=chat[chat[HEAD].back].pro;int pos=HEAD;for(pos=HEAD;pos!=TAIL;pos=chat[pos].back){if(chat[pos].pro==u) break;}chat[pos].w+=w;
}void Rotate(int x)
{if(x<1||x>pc.size()){puts("out of range.");return ;}puts("success.");int pos=HEAD,i=0;for(pos=HEAD;i<x&&pos!=TAIL;pos=chat[pos].back,i++) ;///splitint bc=chat[pos].back;int ft=chat[pos].front;chat[bc].front=ft;chat[ft].back=bc;///mergechat[pos].front=HEAD;chat[pos].back=chat[HEAD].back;chat[chat[HEAD].back].front=pos;chat[HEAD].back=pos;
}void Prior()
{if(chat[HEAD].back==TAIL){puts("empty.");return ;}puts("success.");int pos=HEAD;int mxp=HEAD;for(pos=HEAD;pos!=TAIL;pos=chat[pos].back){if(chat[pos].pro>chat[mxp].pro){mxp=pos;}}pos=mxp;///splitint bc=chat[pos].back;int ft=chat[pos].front;chat[bc].front=ft;chat[ft].back=bc;///mergechat[pos].front=HEAD;chat[pos].back=chat[HEAD].back;chat[chat[HEAD].back].front=pos;chat[HEAD].back=pos;
}void Choose(int u)
{if(pc.count(u)==0){puts("invalid priority.");return ;}puts("success.");int pos=HEAD;for(pos=HEAD;pos!=TAIL;pos=chat[pos].back){if(chat[pos].pro==u) break;}///splitint bc=chat[pos].back;int ft=chat[pos].front;chat[bc].front=ft;chat[ft].back=bc;///mergechat[pos].front=HEAD;chat[pos].back=chat[HEAD].back;chat[chat[HEAD].back].front=pos;chat[HEAD].back=pos;
}void Top(int u)
{if(pc.count(u)==0){puts("invalid priority.");return ;}puts("success.");alwayson=u;
}void Untop()
{if(alwayson==-1){puts("no such person.");return ;}alwayson=-1;puts("success.");
}void saybyebye()
{if(alwayson!=-1){int p=HEAD;for(p=HEAD;p!=TAIL;p=chat[p].back){if(chat[p].pro==alwayson) break;}if(chat[p].w) printf("Bye %d: %I64d\n",chat[p].pro,chat[p].w);}int pos=HEAD;for(pos=chat[HEAD].back;pos!=TAIL;pos=chat[pos].back){if(chat[pos].pro!=alwayson&&chat[pos].w)printf("Bye %d: %I64d\n",chat[pos].pro,chat[pos].w);}
}int main()
{int T_T;scanf("%d",&T_T);while(T_T--){init();scanf("%d",&n);for(int i=0;i<n;i++){scanf("%s",cmd);printf("Operation #%d: ",i+1);if(strcmp(cmd,"Add")==0){int u; scanf("%d",&u);Add(u);}else if(strcmp(cmd,"Close")==0){int u; scanf("%d",&u);Close(u);}else if(strcmp(cmd,"Chat")==0){int u; scanf("%d",&u);Chat(u);}else if(strcmp(cmd,"Rotate")==0){int u; scanf("%d",&u);Rotate(u);}else if(strcmp(cmd,"Prior")==0){Prior();}else if(strcmp(cmd,"Choose")==0){int u; scanf("%d",&u);Choose(u);}else if(strcmp(cmd,"Top")==0){int u; scanf("%d",&u);Top(u);}else if(strcmp(cmd,"Untop")==0){Untop();}}saybyebye();}return 0;
}


转载于:https://www.cnblogs.com/zfyouxi/p/5155798.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/471108.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

十八、MySQL之TCL事务控制语言(详解)

TCL &#xff1a; Transaction Control Language 事务控制语言 零、MySQL 中的存储引擎 1、概念&#xff1a;在mysql中的数据用各种不同的技术存储在文件&#xff08;或内存&#xff09;中。 2、通过show engines&#xff1b;来查看mysql支持的存储引擎。 3、 在mysql中用的…

LeetCode 2220. 转换数字的最少位翻转次数(位运算)

文章目录1. 题目2. 解题1. 题目 一次 位翻转 定义为将数字 x 二进制中的一个位进行 翻转 操作&#xff0c;即将 0 变成 1 &#xff0c;或者将 1 变成 0 。 比方说&#xff0c;x 7 &#xff0c;二进制表示为 111 &#xff0c;我们可以选择任意一个位&#xff08;包含没有显示…

LeetCode 2221. 数组的三角和

文章目录1. 题目2. 解题1. 题目 给你一个下标从 0 开始的整数数组 nums &#xff0c;其中 nums[i] 是 0 到 9 之间&#xff08;两者都包含&#xff09;的一个数字。 nums 的 三角和 是执行以下操作以后最后剩下元素的值&#xff1a; nums 初始包含 n 个元素。如果 n 1 &…

移动web开发之rem布局(rem基础、媒体查询、 less 基础、rem适配方案)

移动web开发之rem布局 一、rem基础 rem单位 rem (root em)是一个相对单位&#xff0c;类似于em&#xff0c;em是父元素字体大小。 不同的是rem的基准是相对于html元素的字体大小。 比如&#xff0c;根元素&#xff08;html&#xff09;设置font-size12px; 非根元素设置wid…

LeetCode 2222. 选择建筑的方案数

文章目录1. 题目2. 解题1. 题目 给你一个下标从 0 开始的二进制字符串 s &#xff0c;它表示一条街沿途的建筑类型&#xff0c;其中&#xff1a; s[i] 0 表示第 i 栋建筑是一栋办公楼&#xff0c;s[i] 1 表示第 i 栋建筑是一间餐厅。 作为市政厅的官员&#xff0c;你需要随…

LeetCode 2224. 转化时间需要的最少操作数(贪心)

文章目录1. 题目2. 解题1. 题目 给你两个字符串 current 和 correct &#xff0c;表示两个 24 小时制时间 。 24 小时制时间 按 "HH:MM" 进行格式化&#xff0c;其中 HH 在 00 和 23 之间&#xff0c;而 MM 在 00 和 59 之间。 最早的 24 小时制时间为 00:00 &…

移动端WEB开发之响应式布局(响应式开发原理、bootstrap、阿里百秀案例)

移动端WEB开发之响应式布局 1.1 响应式开发原理 就是使用媒体查询针对不同宽度的设备进行布局和样式的设置&#xff0c;从而适配不同设备的目的。 设备的划分情况&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8" /><…

jQuery学习笔记系列(三)——事件注册、事件处理、事件对象、拷贝对象、多库共存、jQuery插件、toDoList综合案例

day03 - jQuery 学习目标&#xff1a; 能够说出4种常见的注册事件 能够说出 on 绑定事件的优势 能够说出 jQuery 事件委派的优点以及方式 能够说出绑定事件与解绑事件 能够说出 jQuery 对象的拷贝方法 能够说出 jQuery 多库共存的2种方法 能够使用 jQuery 插件 1.1. jQuery 事件…

LeetCode 2225. 找出输掉零场或一场比赛的玩家(计数)

文章目录1. 题目2. 解题1. 题目 给你一个整数数组 matches 其中 matches[i] [winneri, loseri] 表示在一场比赛中 winneri 击败了 loseri 。 返回一个长度为 2 的列表 answer &#xff1a; answer[0] 是所有 没有 输掉任何比赛的玩家列表。answer[1] 是所有恰好输掉 一场 比…

LeetCode 2226. 每个小孩最多能分到多少糖果(二分查找)

文章目录1. 题目2. 解题1. 题目 给你一个 下标从 0 开始 的整数数组 candies 。数组中的每个元素表示大小为 candies[i] 的一堆糖果。你可以将每堆糖果分成任意数量的 子堆 &#xff0c;但 无法 再将两堆合并到一起。 另给你一个整数 k 。你需要将这些糖果分配给 k 个小孩&am…

指令系统——数据寻址(3)——堆栈寻址(详解)

一、总览 二、堆栈寻址 堆栈寻址&#xff1a;操作数存放在堆栈中&#xff0c;隐含使用堆栈指针&#xff08;SP&#xff09;作为操作数地址。堆栈是存储器&#xff08;或专用寄存器组&#xff09;中一块特定的按“后进先出&#xff08;LIFO&#xff09;” 原则管理的存储区&…

指令系统 CISC和RISC(详解)

一、总览 二、CISC CISC:Complex Instruction Set Computer设计思路&#xff1a;一条指令完成一个复杂的基本功能。代表&#xff1a;86架构&#xff0c;主要用于笔记本、台式机等。 80-20规律&#xff1a;典型程序中80%的语句仅仅使用处理机中20%的指令 三、RISC RISC:Reduc…

LeetCode 2231. 按奇偶性交换后的最大数字

文章目录1. 题目2. 解题1. 题目 给你一个正整数 num 。你可以交换 num 中 奇偶性 相同的任意两位数字&#xff08;即&#xff0c;都是奇数或者偶数&#xff09;。 返回交换 任意 次之后 num 的 最大 可能值。 示例 1&#xff1a; 输入&#xff1a;num 1234 输出&#xff1a…

亲密接触Redis-第一天

引言nosql&#xff0c;大规模分布式缓存遍天下&#xff0c;Internet的时代在中国由其走得前沿&#xff0c;这一切归功于我国特色的电商。因此nosql、大数据技术在中国应用的比国外还要前沿。从这一章开始我们将开始进入到真正的SOA、PAAS、SAAS、互联网的领域&#xff0c;因此每…

Ajax 编程基础(一)

一、Ajax 基础 传统网站中存在的问题&#xff1a; 网速慢的情况下&#xff0c;页面加载时间长&#xff0c;用户只能等待表单提交后&#xff0c;如果一项内容不合格&#xff0c;需要重新填写所有表单内容页面跳转&#xff0c;重新加载页面&#xff0c;造成资源浪费&#xff0c…

LeetCode 2232. 向表达式添加括号后的最小结果

文章目录1. 题目2. 解题1. 题目 给你一个下标从 0 开始的字符串 expression &#xff0c;格式为 "<num1><num2>" &#xff0c;其中 <num1> 和 <num2> 表示正整数。 请你向 expression 中添加一对括号&#xff0c;使得在添加之后&#xff0…

一、服务端开发基础(搭建Web服务器、网络基础概念、请求响应流程、配置Apache、静态网站与动态网站)

一、建立你的第一个网站&#xff08;目标&#xff09; 前端开发 最终还是属于 Web 开发 中的一个分支&#xff0c;想要成为一名合格的前端开发人员&#xff0c;就必须要 充分理解Web 的概念。 构建一个专业的网站是一项巨大的工作&#xff01;对于新手我们应该从小事做起&#…

LeetCode 2233. K 次增加后的最大乘积(优先队列)

文章目录1. 题目2. 解题1. 题目 给你一个非负整数数组 nums 和一个整数 k 。每次操作&#xff0c;你可以选择 nums 中 任一 元素并将它 增加 1 。 请你返回 至多 k 次操作后&#xff0c;能得到的 nums的 最大乘积 。由于答案可能很大&#xff0c;请你将答案对 10^9 7 取余后…

Packet Tracer 5.0 建构 CCNA 实验攻略——配置单区域 OSPF

一、拓扑结构如下&#xff1a; 二、分别为PC0、PC1配置IP地址&#xff0c;子网掩码和默认网关。 三、分别为路由器0、路由器1和路由器2各个接口配置IP地址和子网掩码&#xff0c;并将路由器的开关打开。 注意&#xff1a;可以通过命令show ip route 来查看路由表信息 路由器1和…

NSBundle

bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle. 1、获取bundle (NSBundle *)mainBundle; //app的目录 NSBundle *file [NSBundle mainBundle]; NSLog(&…