题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5071
题目大意:
CLJ和许多妹子聊天(看做是队列),ADD操作是把一个妹子加到队尾,CLOSE是删除一个妹子,CHAT是和队首的妹子说话,ROTATE是把指定位置的妹子移到队首,PRIOR是把ID最大的妹子移到队首,CHOOSE是把指定ID 的妹子移到队首,TOP是置顶某个妹子,UNTOP是撤销置顶。
需要注意的是:
1.TOP视为某种状态(题目中有一句大概意思是,位于队首的妹子被视为在顶端,当没有置顶的妹子时),也就是TOP不改变队列的逻辑位置,只是某个妹子在聊天的时候要把她放到第一个。
2.在最后,要和每个说过话的妹子道别,TOP的妹子优先。这里没看到那句说过话的限制。。。WA了好几次最后才过。
代码有点长,用双向链表写的。
//
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<vector>
#include<map>
#define FOR(i,s,t) for(int i=(s);i<=(t);i++)
#define CFOR(i) for(;i>0;i--)
#define DFOR(i,s,t) for(int i=(s);i>=(t);i--)
#define PFOR(i,s,t,p) for(int i=(s);i<=(t);i+=p)
#define SD(i) scanf("%d",&i)
#define SLLD(i) scanf("%lld",&i)
#define MEM(a,t) memset(a,t,sizeof(a))
#define PIA system("pause")
#define MAXINT 0xfffffff
#define MARK cout<<"MARK"<<endl;
#define BUG cout<<"error!"<<endl;
#define LL long long
using namespace std;
ifstream fin ("in.txt");
ofstream fout ("out2.txt");
#ifdef _WIN32 // FUCK OLD MSVC & LATEST MINGW
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
/********************************************************************************************/
struct NODE{
int L,R;
LL sum,pri;
bool tp;
// NODE(){L=R=sum=0;}
};
/********************************************************************************************/
NODE a[5010];
int h;
int t;
int tot;
void solve(){
char s[10];LL x;
int Q;scanf("%d",&Q);
int top_idx=-1;
int cas=0;
//pos.clear();
CFOR(Q){
printf("Operation #%d: ",++cas);
/*
fout<<endl;
fout<<"queue:"<<endl;
for(int i=h;i!=-1;i=a[i].R){
fout<<"idx="<<i<<",a[i].pri="<<a[i].pri<<endl;
fout<<"a[i].L:"<<a[i].L<<" ,a[i].R = "<<a[i].R<<endl;
}
fout<<"end"<<endl<<endl;*/
scanf("%s",s);
if( s[0]=='A' ){
scanf("%I64d",&x);
bool flag=0;
for(int i=t;i!=-1;i=a[i].L) if( a[i].pri == x ){flag=1;break;}
if(flag){printf("same priority.\n");continue;}
printf("success.\n");
if( h==0 ){
tot++;h=tot;
a[tot].L=a[tot].R=-1;
a[tot].pri=x;
t=tot;
}
else{//队尾加x
tot++;
a[t].R = tot;
a[tot].pri = x;
a[tot].L = t;
a[tot].R = -1;
t=tot;
}
}
else if( s[0]=='C'&&s[1]=='l' ){
scanf("%I64d",&x);
bool flag=0;
int i;
for(i=t;i!=-1;i=a[i].L){
if( a[i].pri == x ){
if( top_idx == i ) a[i].tp=0,top_idx=-1;//
printf("close %I64d with %I64d.\n",x,a[i].sum);
flag=1;
break;
}
}
if(!flag) {printf("invalid priority.\n");continue;}
if( a[i].L==-1&&a[i].R==-1 ) h=0;
else{
if( a[i].L==-1 ){
h=a[i].R;
a[h].L = -1;
}
else if( a[i].R==-1 ){
t=a[i].L;
a[t].R = -1;
}
else{
a[a[i].L].R = a[i].R;
a[a[i].R].L = a[i].L;
}
}
}//
else if( s[0]=='C'&&s[1]=='h'&&s[2]=='a' ){
// cout<<"MARK"<<endl;
scanf("%I64d",&x);
if( h==0 ){
printf("empty.\n");
}
else{
printf("success.\n");
if(top_idx==-1) a[h].sum += x;
else a[top_idx].sum += x;//
}
}
else if( s[0]=='R'){
scanf("%I64d",&x);
if( x<=0 ) {printf("out of range.\n");continue;}
if( x==1 ) {printf("success.\n");continue;}
// if( top_idx!=-1 ) x--;//
// if( x==1 ) {printf("success.\n");continue;}//
int cnt=0,i;
for(i=h;i!=-1;i=a[i].R){
cnt++;
if( cnt==x ) break;
}
if( cnt==x ){
//
if( a[i].R==-1 ){
a[a[i].L].R = -1;
t = a[i].L;
}
else{
a[a[i].L].R = a[i].R;
a[a[i].R].L = a[i].L;
}
a[i].R = h;
a[i].L = -1;
a[h].L = i;
h = i;
printf("success.\n");
}
else printf("out of range.\n");
}
else if( s[0]=='P'){
if( h==0 ){
printf("empty.\n");
}
else{
printf("success.\n");
LL max_pri=0,idx=0;
for(int i=h;i!=-1;i=a[i].R){
if( a[i].pri > max_pri ){
max_pri = a[i].pri;
idx = i;
}
}
if( h==idx ){}
else{
if( a[idx].R == -1 ){
a[a[idx].L].R = -1;
t = a[idx].L;
}
else{
a[a[idx].L].R = a[idx].R;
a[a[idx].R].L = a[idx].L;
}
a[idx].R = h;
a[idx].L = -1;
a[h].L = idx;
h=idx;
}
}
}
else if( s[0]=='C'&&s[1]=='h'&&s[2]=='o' ){
scanf("%I64d",&x);
int idx = -1;
for(int i=h;i!=-1;i=a[i].R){
if( a[i].pri == x ){
idx = i;
break;
}
}
if( idx==-1 ) printf("invalid priority.\n");
else{
printf("success.\n");
if(idx==h){}
else{
if( a[idx].R == -1 ){
a[a[idx].L].R = -1;
t = a[idx].L;
}
else{
a[a[idx].L].R = a[idx].R;
a[a[idx].R].L = a[idx].L;
}
a[idx].R = h;
a[idx].L = -1;
a[h].L = idx;
h=idx;
}
}
}
else if( s[0]=='T'){
scanf("%I64d",&x);
int idx = -1;
for(int i=h;i!=-1;i=a[i].R){
if( a[i].pri == x ){
idx = i;
break;
}
}
if( idx==-1 ) printf("invalid priority.\n");
else{
printf("success.\n");
a[top_idx].tp=0;
a[idx].tp=1;
top_idx=idx;
}
}
else if( s[0]=='U'){
if( top_idx==-1 ){
printf("no such person.\n");
}
else{
printf("success.\n");
a[top_idx].tp=0;
top_idx=-1;
}
}
}
//
if(h==0){}
else{
if( top_idx!=-1 ){
if( a[top_idx].sum==0 ) {}
else printf("Bye %I64d: %I64d\n",a[top_idx].pri,a[top_idx].sum);
}
for(int i=h;i!=-1;i=a[i].R){
if(top_idx == i)continue;
if( a[i].sum==0 ) {}
else printf("Bye %I64d: %I64d\n",a[i].pri,a[i].sum);
}
}
}
void init(){
FOR(i,0,5000) a[i].sum=a[i].tp=0,a[i].L=a[i].R=-1;
h=0;tot=0;t=0;
}
void openfile(){
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
}
int main(){
//openfile();
int T;scanf("%d",&T);
CFOR(T){
init();
solve();
}
//PIA;
return 0;
}