4月模拟-TT与可怜的猫
程序设计思维实践-复杂模拟题训练2
山东大学计算机科学与技术学院程序设计思维与实践
sdu程序设计思维与实践
山东大学程序设计思维实践作业
山大程序设计思维实践
山东大学程序设计思维与实践
TT与可怜的猫
题目描述
自从 TT 成为了助教,他就热衷于给同学们解答疑问,于是他就没有时间进行陪他的猫猫玩了,真是一只可怜的小猫。
TTandCat.png
TT 在同一时间会与很多同学同时用 QQ(TT 特供版) 进行答疑,有时 TT 开启一个新的窗口,开始一个新的答疑对话;有时 TT 关闭一个窗口,结束一段答疑; 有时,为了及时回答着急的同学,TT 会把一个对话窗口设置为置顶状态(置顶状态是一种虚拟的状态,窗口的实际位置并不会发生改变)。
你可以将 TT 的聊天窗口想象成一个队列。如果现在没有窗口处于置顶状态,那么在队列中位列第一窗口视为在顶层,否则处于置顶状态的窗口视为在顶层。请注意,不可能同时存在两个窗口处于置顶状态(也就是说,处于置顶状态的窗口,要么不存在,要么只有一个)。如果当前置顶的窗口被关闭了,则剩余队列中第一窗口视为在顶层。
TT 为了安抚自己的猫,于是给猫猫看自己的聊天记录解闷,根据猫猫看屏幕中窗口的时间,TT 给每个窗口分配了一个喜爱度,TT 认为喜爱度越高,猫猫越开心。由于猫猫具有不确定的生物特性,所以所有的喜爱度都是不同的。
作为 TT 特供版 QQ 的研发人员,你要负责完成的工作是为软件记录 TT 的操作,形成一个日志系统。日志有固定的记录格式:OpId #X: MSG. ,其中 X 是操作的编号,而 MSG 是日志的提示信息,需要使用特定的字符串进行替换。
TT 可能会用到的操作如下:
Add u: TT 打开一个喜爱度为 u 的新窗口,若 u 不与当前窗口队列中的某个窗口重复,则该新窗口将新建成功,并成为窗口队列中的最后一个窗口。 如果创建成功,则 MSG 记录 success。 否则, MSG 记录 same likeness 。
Close u: TT 关掉了一个喜爱度为 u 的窗口,如果该窗口存在,则将其关闭,MSG 记录 close u with c,u 表示喜爱度,c 表示该窗口上次打开至今交流的话的数量。若该窗口不存在,则 MSG 记录 invalid likeness。
Chat w: TT 和顶层窗口交流了 w 句话,如果当前队列中没有窗口,则 MSG 记录 empty,否则记录 success。
Rotate x: 将队列中第 x 个窗口放在队首,若 x 大于当前队列中窗口数或小于 1 ,则 MSG 记录 out of range,否则记录 success。举个例子,目前队列中有喜爱度为 5,3,2,8 的四个窗口,Rotate 3 之后,会将喜爱度为 2 的第 3 个窗口放在首位,结果为 2,5,3,8。
Prior: TT 将目前喜爱度最大的窗口放在队首,如果当前队列中没有窗口,则 MSG 记录 empty,否则记录 success。
Choose u: TT 将喜爱度为 u 的窗口放在队首,如果喜爱度为 u 的窗口存在,则 MSG 记录 success,否则记录 invalid likeness。
Top u: TT 将喜爱度为 u 的窗口设定为置顶状态,如果喜爱度为 u 的窗口存在,则 MSG 记录 success,否则记录 invalid likeness。注意,处于置顶状态的窗口最多不超过一个,也就是说,如果在此次设定前已经有处于置顶状态的窗口,则原有置顶状态的窗口的置顶状态将会消失。(置顶只是一种虚拟的状态,原窗口在队列中的位置不会发生变化)
Untop: TT 取消当前处于置顶状态窗口的置顶状态。如果当前没有窗口处于置顶状态,则 MSG 记录 no such person,否则记录 success。
最后,由于 TT 要给自己的猫猫树立一个讲文明有礼貌的榜样,所以在上述操作完成后,还要进行若干次操作,这些操作是:与当前队列中所有说过话的窗口说拜拜。MSG 记录 Bye u: c, u 表示喜爱度,c 表示该窗口上次打开至今交流的话的数量。即:TT 先和位于顶层的窗口说拜拜,然后将其关闭,如果 TT 没有和当前顶层窗口说过话,则直接将其关闭,如此操作下去,直到队列为空。
输入描述
第一行包含一个整数 T(T≤5),表示数据组数。
对于每组数据,第一行一个 n,表示执行的操作数,其中 0<n≤5000。接下来 n 行,每行输入一个操作,保证所有输入数据中的整数不大于 10
9
。
输出描述
对于每个指定的操作,按照日志的格式,每个操作行。对于最后的非指定操作,同样按照日志的格式,每个操作一行。
测试样例
样例 1
输入:
1
30
Add 4
Add 3
Chat 4
Add 3
Rotate 2
Chat 5
Prior
Top 3
Choose 4
Rotate 1
Add 2
Close 4
Chat 7
Choose 2
Chat 7
Add 3
Top 2
Add 4
Choose 3
Chat 7
Prior
Top 3
Rotate 1
Rotate 3
Chat 7
Top 4
Add 2
Close 2
Prior
Add 4
输出:
OpId #1: success.
OpId #2: success.
OpId #3: success.
OpId #4: same likeness.
OpId #5: success.
OpId #6: success.
OpId #7: success.
OpId #8: success.
OpId #9: success.
OpId #10: success.
OpId #11: success.
OpId #12: close 4 with 4.
OpId #13: success.
OpId #14: success.
OpId #15: success.
OpId #16: same likeness.
OpId #17: success.
OpId #18: success.
OpId #19: success.
OpId #20: success.
OpId #21: success.
OpId #22: success.
OpId #23: success.
OpId #24: success.
OpId #25: success.
OpId #26: success.
OpId #27: same likeness.
OpId #28: close 2 with 7.
OpId #29: success.
OpId #30: same likeness.
OpId #31: Bye 3: 26.
#include<iostream>
#include<deque>
#include<string>
using namespace std;
typedef long long ll;
class win {
public:
win(int t) { this->u = t; this->lxcishu = 0; }//新建留言
int u;//喜好度
ll lxcishu;//留言次数
bool up = 0;//判断当前窗口是否置顶
};
deque<win> windows;//所有窗口
deque<win> temp;//缓冲区
bool ADD(int u) {//add u操作 打开一个喜爱度为u的新窗口
for (auto it = windows.begin(); it != windows.end(); it++) {//查看一下有没有好感度为u的
if ((*it).u == u) {
return 0;
}
}
win New_win(u);
windows.push_back(New_win);//尾插入新的窗口
return 1;
}
void CLOSE(int u) {
bool flag = 0;
ll lxcishu;
for (auto i = windows.begin(); i != windows.end(); i++) {//查看一下有没有好感度为u的
if ((*i).u == u) {
flag = 1;
lxcishu = (*i).lxcishu;
break;
}
}
if (flag == 0) { cout << "invalid likeness."<<endl; return; }//如果没找到
auto it = windows.begin();
while ((*it).u!=u) {//把目标之前的都弹出,并存起来
temp.push_front(*it);
windows.pop_front();
it = windows.begin();
}
windows.pop_front();//删除目标
if (temp.empty()) {//如果要删的是第一个
cout << "close " << u << " with " << lxcishu << "." << endl;
return;
}
while(!temp.empty()){//把原先的再重新放回去
auto iit = temp.begin();
windows.push_front(*iit);
temp.pop_front();
}
cout << "close " << u << " with " << lxcishu << "." << endl;
}
void Chat(ll w) {
if (windows.empty()) {
cout << "empty." << endl; return;
}
for (auto it = windows.begin(); it != windows.end(); it++) {
if ((*it).up == 1) {
(*it).lxcishu += w;
cout << "success." << endl;
return;
}
}
windows.front().lxcishu += w;
cout << "success." << endl;
}
void Rotate(int x) {
if (x > windows.size() || x < 1) {
cout << "out of range." << endl;
return;
}
for (int i = 0; i < x; i++) {//把第x以及之前的都弹出,并存起来
auto it = windows.begin();
temp.push_front(*it);
windows.pop_front();
}
win new_top = *(temp.begin());
temp.pop_front();//把要新放入队首的拿出来
for (int i = 1; i < x; i++) {//把原先的再重新放回去
auto iit = temp.begin();
windows.push_front(*iit);
temp.pop_front();
}
windows.push_front(new_top);
cout << "success." << endl;
}
void Prior() {
if (windows.empty()) {
cout << "empty." << endl;
return;
}
ll Max_love = 0;
for (auto it = windows.begin(); it != windows.end(); it++) {
if ((*it).u > Max_love)Max_love = (*it).u;
}
auto it = windows.begin();
while (!windows.empty()&&(*it).u<Max_love) {//把第Max_love之前的都弹出,并存起来
temp.push_front(*it);
windows.pop_front();
it = windows.begin();
}
temp.push_front(*it);
windows.pop_front();
win new_top = *(temp.begin());
temp.pop_front();//把要新放入队首的拿出来
while(!temp.empty()) {//把原先的再重新放回去
auto iit = temp.begin();
windows.push_front(*iit);
temp.pop_front();
}
windows.push_front(new_top);
cout << "success." << endl;
}
void Choose(int u) {
bool flag = 0;
for (auto it = windows.begin(); it != windows.end(); it++) {//查看一下有没有好感度为u的
if ((*it).u == u) {
flag = 1;
break;
}
}
if (flag == 0) { cout << "invalid likeness."<<endl; return; }//如果没找到
auto it = windows.begin();
while((*it).u!=u) {//把好感u之前的都弹出,并存起来
temp.push_front(*it);
windows.pop_front();
it = windows.begin();
}
temp.push_front(*it);
windows.pop_front();
it = windows.begin();
if (temp.empty()) { cout << "success." << endl; return; }//如果一开始就是队首
win new_top = *(temp.begin());
temp.pop_front();//把要新放入队首的拿出来
while(!temp.empty()) {//把原先的再重新放回去
auto iit = temp.begin();
windows.push_front(*iit);
temp.pop_front();
}
windows.push_front(new_top);
cout << "success." << endl;
}
void Top(int u) {
bool flag = 0;
for (auto it = windows.begin(); it != windows.end(); it++) {//查看一下有没有好感度为u的
if ((*it).u == u) {
flag = 1;
break;
}
}
if (flag == 0) { cout << "invalid likeness."<<endl; return; }//如果没找到
for (auto it = windows.begin(); it != windows.end(); it++) {//将好感度为u的置顶,并且把之前置顶的删了
if ((*it).u != u && (*it).up == 1) { (*it).up = 0; }
if ((*it).u == u) {
(*it).up = 1;//标记置顶
}
}
cout << "success." << endl;
}
void Untop() {
bool flag = 0;
for (auto it = windows.begin(); it != windows.end(); it++) {//查看一下有没有置顶的
if ((*it).up == 1) {
flag = 1;
(*it).up = 0;
}
}
if (flag == 0) { cout << "no such person." << endl; return; }//如果没找到
cout << "success." << endl;
}
void Bye(int j) {
//如果有置顶的
for (auto it = windows.begin(); it != windows.end(); it++) {
if ((*it).up == 1)
{
if ((*it).lxcishu != 0) {
cout << "OpId #" << j << ": ";
j++;
cout << "Bye " << (*it).u << ": " << (*it).lxcishu << "." << endl;
}
break;
}
}
while (!windows.empty()) {
auto it = windows.begin();
if ((*it).up != 1 && (*it).lxcishu != 0) {//聊过天但没有置顶的同学
cout << "OpId #" << j << ": ";
j++;
cout << "Bye " << (*it).u << ": " << (*it).lxcishu << "." << endl;
}
windows.pop_front();
}
}
int main() {
//FILE* s1;
//freopen_s(&s1, "input_1.txt", "r", stdin);
//freopen_s(&s1, "out_1.txt", "w", stdout);
int T;
cin >> T;
for (int t = 0; t < T; t++) {
int n;
cin >> n;
int j = 1;
for (; j <= n; j++) {//n+1次操作,最后一次操作为bye
string op;
cin >> op;
//Add操作
if (op == "Add") {//Add u操作
int num;
cin >> num;
if (ADD(num) == 1)cout << "OpId #" << j << ": success." << endl;
else cout << "OpId #" << j << ": same likeness." << endl;
}
//Close操作
else if (op == "Close") {
int u;
cin >> u;
cout << "OpId #" << j << ": ";
CLOSE(u);
}
//Chat操作
else if (op == "Chat") {
ll w;
cin >> w;
cout << "OpId #" << j << ": ";
Chat(w);
}
//Rotate操作
else if (op == "Rotate") {
int x;
cin >> x;
cout << "OpId #" << j << ": ";
Rotate(x);
}
//Piror操作
else if (op == "Prior") {
cout << "OpId #" << j << ": ";
Prior();
}
//Choose操作
else if (op == "Choose") {
int u;
cin >> u;
cout << "OpId #" << j << ": ";
Choose(u);
}
//Top操作
else if (op == "Top") {
int u;
cin >> u;
cout << "OpId #" << j << ": ";
Top(u);
}
//Untop操作
else if (op == "Untop") {
cout << "OpId #" << j << ": ";
Untop();
}
}
//bye操作
Bye(j);
windows.clear();//清空
temp.clear();
}
}