山东大学程序设计思维-每月复杂模拟2-TT与可怜的猫

#include <bits/stdc++.h>
using namespace std;
struct window{
    long long likeness;
    long long chat_num=0;
    bool if_piror=false;
    window(long long like):likeness(like){};
    window(){};
    bool operator>(const window w) const{
        return likeness>w.likeness;
    }
    bool operator==(const window w) const{
        return likeness==w.likeness;
    }
};
class chatapp{
private:
    set<window,greater<window>> windows;
    vector<window> as_queue;
    long long piror_likeness;
    bool if_piror_exists=false;
    long long reply_num=1;
    long long window_num=0;
    string fixed(){
        return "OpId #"+ to_string(reply_num)+": ";
    }
public:
    void add(long long u){
        window t(u);
        if(windows.insert(t).second){
            as_queue.push_back(t);
            cout<<fixed()+"success"+"."<<endl;
            window_num++;
        }
        else cout<<fixed()+"same likeness"+"."<<endl;
        reply_num++;
    }
    void close(long long u){
        window w(u);
        if(windows.find(w)!=windows.end()){
            windows.erase(w);
            auto t=find(as_queue.begin(),as_queue.end(),w);
            cout<<fixed()+"close "+ to_string(u)+" with "+ to_string(t->chat_num)+"."<<endl;
            if(t->if_piror) if_piror_exists=false;
            as_queue.erase(t);
            window_num--;
        }
        else cout<<fixed()+"invalid likeness"+"."<<endl;
        reply_num++;
    }
    void chat(long long n){
        if(if_piror_exists){
            window w(piror_likeness);
            auto t=find(as_queue.begin(),as_queue.end(),w);
            auto tt=windows.find(w);
            auto ttw=*tt;
            ttw.chat_num+=n;
            windows.erase(tt);
            windows.insert(ttw);
            t->chat_num+=n;
            cout<<fixed()+"success"+"."<<endl;
            reply_num++;
            return;
        }
        if(!as_queue.empty()){
            auto ttw=as_queue[0];
            as_queue[0].chat_num+=n;
            ttw.chat_num+=n;
            auto tt=windows.find(ttw);
            windows.erase(tt);
            windows.insert(ttw);
            cout<<fixed()+"success"+"."<<endl;
        }
        else cout<<fixed()+"empty"+"."<<endl;
        reply_num++;
    }
    void rotate(long long x){
        if(x>window_num||x<1){
            cout<<fixed()+"out of range"+"."<<endl;
            reply_num++;
            return;
        }
        x--;
        auto t=as_queue[x];
        as_queue.erase(as_queue.begin()+x);
        as_queue.insert(as_queue.begin(),t);
        cout<<fixed()+"success"+"."<<endl;
        reply_num++;
    }
    void piror(){
        if(!as_queue.empty()){
            auto u=*windows.begin();
            auto t=find(as_queue.begin(),as_queue.end(),u);
            auto tt=*t;
            as_queue.erase(t);
            as_queue.insert(as_queue.begin(),tt);
            cout<<fixed()+"success"+"."<<endl;
        }
        else cout<<fixed()+"empty"+"."<<endl;
        reply_num++;
    }
    void choose(long long u){
        window w(u);
        auto t=find(as_queue.begin(),as_queue.end(),w);
        if(t!=as_queue.end()){
            auto tt=*t;
            as_queue.erase(t);
            as_queue.insert(as_queue.begin(),tt);
            cout<<fixed()+"success"+"."<<endl;
        }
        else cout<<fixed()+"invalid likeness"+"."<<endl;
        reply_num++;
    }
    void top(long long u){
        window w(u);
        auto t=find(as_queue.begin(),as_queue.end(),w);
        if(t!=as_queue.end()){
            t->if_piror=true;
            if(if_piror_exists){
                window now(piror_likeness);
                auto before=find(as_queue.begin(),as_queue.end(),now);
                before->if_piror=false;
            }
            else if_piror_exists=true;
            auto tt=windows.find(*t);
            auto ttw=*tt;
            ttw.if_piror=true;
            windows.erase(tt);
            windows.insert(ttw);
            piror_likeness=t->likeness;
            cout<<fixed()+"success"+"."<<endl;
        }
        else cout<<fixed()+"invalid likeness"+"."<<endl;
        reply_num++;
    }
    void untop(){
        if(!if_piror_exists) cout<<fixed()+"no such person"+"."<<endl;
        else{
            if_piror_exists=false;
            auto t=find(as_queue.begin(),as_queue.end(),piror_likeness);
            t->if_piror=false;
            auto tt=windows.find(*t);
            auto ttw=*tt;
            ttw.if_piror=true;
            windows.erase(tt);
            windows.insert(ttw);
            cout<<fixed()+"success"+"."<<endl;
        }
        reply_num++;
    }
    void saybye(){
        long long i=0;
        if(if_piror_exists){
            window w(piror_likeness);
            auto t=*find(as_queue.begin(),as_queue.end(),w);
            if(t.chat_num!=0){
                cout<<fixed()+"Bye "+ to_string(t.likeness)+": "+ to_string(t.chat_num)+"."<<endl;
                reply_num++;
            }
        }
        while(i<as_queue.size()){
            auto t=as_queue[i];
            if(t.chat_num==0||t.if_piror) {
                i++;
                continue;
            }
            cout<<fixed()+"Bye "+ to_string(t.likeness)+": "+ to_string(t.chat_num)+"."<<endl;
            reply_num++;
            i++;
        }
    }
};
int main(){
    long long ca;
    cin>>ca;
    for(long long i=0;i<ca;i++){
        chatapp c;
        long long n;
        cin>>n;
        for(long long j=0;j<n;j++){
            string op;
            long long u;
            cin>>op;
            if(op=="Prior"){
                c.piror();
                continue;
            }
            if(op=="Untop"){
                c.untop();
                continue;
            }
            cin>>u;
            if(op=="Add"){
                c.add(u);
                continue;
            }
            if(op=="Close"){
                c.close(u);
                continue;
            }
            if(op=="Chat"){
                c.chat(u);
                continue;
            }
            if(op=="Rotate"){
                c.rotate(u);
                continue;
            }
            if(op=="Choose"){
                c.choose(u);
                continue;
            }
            if(op=="Top"){
                c.top(u);
                continue;
            }
        }
        c.saybye();
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值