#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-TT与可怜的猫
于 2024-04-19 22:08:15 首次发布