四月模拟 : TT与可怜的猫

题目描述

自从 TT 成为了助教,他就热衷于给同学们解答疑问,于是他就没有时间进行陪他的猫猫玩了,真是一只可怜的小猫。

TT 在同一时间会与很多同学同时用 QQ(TT特供版) 进行答疑,有时 TT 开启一个新的窗口,开始一个新的答疑对话;有时 TT 关闭一个窗口,结束一段答疑; 有时,为了及时回答着急的同学,TT会把一个对话窗口设置为置顶状态(置顶状态是一种虚拟的状态,窗口的实际位置并不会发生改变)。

你可以将 TT 的聊天窗口想象成一个队列。如果现在没有窗口处于置顶状态,那么在队列中位列第一窗口视为在顶层,否则处于置顶状态的窗口视为在顶层。请注意,不可能同时存在两个窗口处于置顶状态(也就是说,处于置顶状态的窗口,要么不存在,要么只有一个)。如果当前置顶的窗口被关闭了,则剩余队列中第一窗口视为在顶层。

TT 为了安抚自己的猫,于是给猫猫看自己的聊天记录解闷,根据猫猫看屏幕中窗口的时间,TT 给每个窗口分配了一个喜爱度,TT 认为喜爱度越高,猫猫越开心。由于猫猫具有不确定的生物特性,所以所有的喜爱度都是不同的。

作为 TT 特供版QQ的研发人员,你要负责完成的工作是为软件记录 TT 的操作,形成一个日志系统。日志有固定的记录格式:OpId #X: MSG. ,其中
X
X 是操作的编号,而 MSG 是日志的提示信息,需要使用特定的字符串进行替换。

TT 可能会用到的操作如下:

Add u: TT 打开一个喜爱度为
u
u 的新窗口,若
u
u 不与当前窗口队列中的某个窗口重复,则该新窗口将新建成功,并成为窗口队列中的最后一个窗口。 如果创建成功,则 MSG 记录 success。 否则, MSG 记录 same likeness 。

Close u: TT 关掉了一个喜爱度为
u
u 的窗口,如果该窗口存在,则将其关闭,MSG 记录 close u with c,
u
u 表示喜爱度,
c
c 表示该窗口上次打开至今交流的话的数量。若该窗口不存在,则MSG 记录invalid likeness。

Chat w: TT 和顶层窗口交流了
w
w 句话,如果当前队列中没有窗口,则MSG 记录empty,否则记录 success。

Rotate x: 将队列中第
x
x 个窗口放在队首,若
x
x 大于当前队列中窗口数或小于
1
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 的窗口放在队首,如果喜爱度为
u
u 的窗口存在,则MSG 记录success,否则记录invalid likeness。

Top u: TT 将喜爱度为
u
u 的窗口设定为置顶状态,如果喜爱度为
u
u 的窗口存在,则MSG 记录success,否则记录invalid likeness。注意,处于置顶状态的窗口最多不超过一个,也就是说,如果在此次设定前已经有处于置顶状态的窗口,则原有置顶状态的窗口的置顶状态将会消失。(置顶只是一种虚拟的状态,原窗口在队列中的位置不会发生变化)

Untop: TT 取消当前处于置顶状态窗口的置顶状态。如果当前没有窗口处于置顶状态,则MSG 记录no such person,否则记录success。

最后,由于TT要给自己的猫猫树立一个讲文明有礼貌的榜样,所以在上述操作完成后,还要进行若干次操作,这些操作是:与当前队列中所有说过话的窗口说拜拜。MSG 记录Bye u: c,
u
u 表示喜爱度,
c
c 表示该窗口上次打开至今交流的话的数量。即:TT 先和位于顶层的窗口说拜拜,然后将其关闭,如果TT没有和当前顶层窗口说过话,则直接将其关闭,如此操作下去,直到队列为空。

输入描述

第一行包含一个整数
T
(
T

5
)
T(T≤5),表示数据组数。

对于每组数据,第一行一个
n
n,表示执行的操作数,其中
0
<
n

5000
0<n≤5000。接下来
n
n 行,每行输入一个操作,保证所有输入数据中的整数不大于
1
0
9
10
9

输出描述

对于每个指定的操作,按照日志的格式,每个操作行。对于最后的非指定操作,同样按照日志的格式,每个操作一行。

Case 1
Input
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
Output
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 <map>
#include <list>
#include <cstring>

#define MSG_SUCC "OpId #%d: success.\n"
#define MSG_SMLK "OpId #%d: same likeness.\n"
#define MSG_CLSU "OpId #%d: close %llu with %llu.\n"
#define MSG_IVLK "OpId #%d: invalid likeness.\n"
#define MSG_EMPT "OpId #%d: empty.\n"
#define MSG_OORG "OpId #%d: out of range.\n"
#define MSG_NSPS "OpId #%d: no such person.\n"
#define MSG_BYEE "OpId #%d: Bye %llu: %llu.\n"
#define EMP_MSG "OpId #%d: ."

using std::map;
using std::list;
using std::make_pair;

typedef long long unsigned llu;
char cmmd[10];
llu num;

void read()
{
    scanf("%s", cmmd);
    cmmd[3] = 0;
}

void rnum()
{
    scanf("%llu", &num);
}

class ttWindow
{
public:
    llu chatHistory = 0;
    llu u = 0;
    bool alive = true;
    void swap(ttWindow *nt)
    {
        if (!alive || !nt->alive)
        {
            throw -1;
        }
        llu ex = nt->chatHistory;
        nt->chatHistory = chatHistory;
        chatHistory = ex;
    }
};

void app()
{
    ttWindow *topone = nullptr;
    map<llu, ttWindow*> ulist;
    list<ttWindow*> tlist;

    int n;
    scanf("%d", &n);
    int i = 1;
    for (; i <= n; i++)
    {
        read();
        if (strcmp(cmmd, "Add") == 0)
        {
            rnum();
            if (ulist.find(num) != ulist.end() )
            {
                printf(MSG_SMLK, i);
            }
            else
            {
                ttWindow *nw = new ttWindow();
                nw->u = num;
                ulist.insert(make_pair(num, nw));
                tlist.push_back(nw);
                printf(MSG_SUCC, i);
            }
        }
        if (strcmp(cmmd, "Clo") == 0)
        {
            rnum();
            map<llu, ttWindow*>::iterator it = ulist.find(num);
            if (it != ulist.end())
            {
                printf(MSG_CLSU, i, num, it->second->chatHistory);
                it->second->alive = false;
                if (topone == it->second)
                {
                    topone = nullptr;
                }
                ulist.erase(it);
            }
            else
            {
                printf(MSG_IVLK, i);
            }
        }
        if (strcmp(cmmd, "Cha") == 0)
        {
            rnum();

            bool has = false;
            for (list<ttWindow*>::iterator it = tlist.begin(); topone == nullptr && it != tlist.end(); it++)
            {
                if ((*it)->alive)
                {
                    (*it)->chatHistory += num;
                    has = true;
                    break;
                }
            }
            if (topone != nullptr)
            {
                topone->chatHistory += num;
                has = true;
            }
            if (has)
            {
                printf(MSG_SUCC, i);
            }
            else
            {
                printf(MSG_EMPT, i);
            }
        }
        if (strcmp(cmmd, "Rot") == 0)
        {
            rnum();
            llu ix = num;
            if (num < 1)
            {
                printf(MSG_OORG, i);
            }
            else
            {
                for (list<ttWindow*>::iterator it = tlist.begin(); it != tlist.end();)
                {
                    if ((*it)->alive)
                    {
                        ix--;
                        if (ix == 0)
                        {
                            ttWindow *p = (*it);
                            tlist.erase(it);
                            tlist.push_front(p);
                            break;
                        }
                        it++;
                    }
                    else
                    {
                        list<ttWindow*>::iterator eit = it;
                        it++;
                        delete *eit;
                        tlist.erase(eit);
                    }
                }
                if (ix == 0)
                {
                    printf(MSG_SUCC, i);
                }
                else
                {
                    printf(MSG_OORG, i);
                }
            }
        }
        if (strcmp(cmmd, "Pri") == 0)
        {
            ttWindow *maxu;
            if (ulist.size() == 0)
            {
                printf(MSG_EMPT, i);
            }
            else
            {
                maxu = (--ulist.end())->second;
                for (list<ttWindow*>::iterator it = tlist.begin(); it != tlist.end();)
                {
                    if ((*it)->alive)
                    {
                        if (*it == maxu)
                        {
                            tlist.erase(it);
                            tlist.push_front(maxu);
                            break;
                        }
                        it++;
                    }
                    else
                    {
                        list<ttWindow*>::iterator eit = it;
                        it++;
                        delete *eit;
                        tlist.erase(eit);
                    }
                }
                printf(MSG_SUCC, i);
            }
        }
        if (strcmp(cmmd, "Cho") == 0)
        {
            rnum();
            map<llu, ttWindow*>::iterator it = ulist.find(num);
            if (it == ulist.end())
            {
                printf(MSG_IVLK, i);
            }
            else
            {
                ttWindow *cu = it->second;
                for (list<ttWindow*>::iterator it = tlist.begin(); it != tlist.end();)
                {
                    if ((*it)->alive)
                    {
                        if (*it == cu)
                        {
                            tlist.erase(it);
                            tlist.push_front(cu);
                            break;
                        }
                        it++;
                    }
                    else
                    {
                        list<ttWindow*>::iterator eit = it;
                        it++;
                        delete *eit;
                        tlist.erase(eit);
                    }
                }
                printf(MSG_SUCC, i);
            }
        }
        if (strcmp(cmmd, "Top") == 0)
        {
            rnum();
            map<llu, ttWindow*>::iterator it = ulist.find(num);
            if (it == ulist.end())
            {
                printf(MSG_IVLK, i);
            }
            else
            {
                topone = it->second;
                printf(MSG_SUCC, i);
            }
        }
        if (strcmp(cmmd, "Unt") == 0)
        {
            if (topone == nullptr)
            {
                printf(MSG_NSPS, i);
            }
            else
            {
                topone = nullptr;
                printf(MSG_SUCC, i);
            }
        }
    }
    if (topone)
    {
        if (topone->chatHistory)
        {
            printf(MSG_BYEE, i,topone->u, topone->chatHistory);
            i++;
        }
        topone->alive = false;
    }
    while (tlist.size())
    {
        if ((*(tlist.begin()))->alive && (*(tlist.begin()))->chatHistory)
        {
            printf(MSG_BYEE, i, (*(tlist.begin()))->u, (*(tlist.begin()))->chatHistory);
            i++;
        }
        delete (*(tlist.begin()));
        tlist.erase(tlist.begin());
    }
}

int main()
{
    int m;
    scanf("%d", &m);

    for (int i = 0; i < m; i++)
    {
        app();
    }

    return 0;
}

在这里插入图片描述

这题也不怎么会做,这些是可能用到的操作,按他的要求写到函数代码里。
在这里插入图片描述

需要用到几个数据结构:
map是一个以键值对形式存储元素的容器;
list 容器是由双向链表实现的,因此不能使用下标运算符 [] 访问其中的元素;
std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型;
make_pair的用法:
无需写出型别, 就可以生成一个pair对象;

这几个链接是详细讲解:
pair部分
list部分
map部分

这题还需要创建类class:在这里插入图片描述
以后可以访问这些公有成员;

还要写个函数来实现“TT 特供版QQ”。
虽然没有很难理解的算法,但比较复杂,超出我的范围,得参考网上和别人的代码写写。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值