是2014鞍山赛区的一道银牌题。有这几个坑点:
1.删掉某个窗口时,若此窗口时alwaystop,需要把alwaystop清零,还要把cnt清零,因为下次可能还打开此窗口和她聊天
2.最后说Bey的时候,先对alwaystop说Bey,再对队列里的依次说Bey
3.只有和她聊过天,才对她说Bey,否则不说
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
#define PB push_back
#define MP make_pair
typedef pair<int,int> pii;
template <class T>
inline bool RD(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1 , ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void PT(T x) {
if (x < 0) putchar('-') ,x = -x;
if (x > 9) PT(x / 10);
putchar(x % 10 + '0');
}
const int N = 5e3 + 100;
map<int,int> mp;
int que[N];
ll cnt[N];
int ache[N];
int top, all;
int aws;
inline void gotop(int pos) {
int tmp = que[pos];
for(int i = pos; i > 1; i --) que[i] = que[i-1];
que[1] = tmp;
}
inline void del(int pos) {
for(int i = pos; i < top; i ++) que[i] = que[i + 1];
top --;
}
int main() {
int T;
RD(T);
while( T -- ) {
aws = 0;
memset(cnt, 0, sizeof(cnt));
mp.clear();
int Q;
RD(Q);
top = all = 0;
REP(cas, Q){
printf("Operation #%d: ",cas);
char op[15];
scanf("%s", op);
if( op[0] == 'A') {
int val;
RD(val);
bool flag = 0;
if( mp.count(val) ) {
int u = mp[val];
REP(i, top) {
if(que[i] == u) {
flag = 1;
break;
}
}
}
if(flag) {
puts("same priority.");
continue;
}
puts("success.");
if(mp.count(val) == 0) {
mp[val] = ++all;
ache[all] = val;
}
que[++top] = mp[val];
} else if( op[0] == 'R' ) {
int x;
RD(x);
if( x < 1 || x > top) {
puts("out of range.");
continue;
}
puts("success.");
gotop(x);
} else if( op[0] == 'P') {
int maxn = -1, pos = -1;
REP(i, top) if( maxn < ache[que[i]]) maxn = ache[que[i]], pos = i;
if(maxn == -1 && top == 0) {
puts("empty.");
continue;
}
puts("success.");
gotop(pos);
} else if( op[0] == 'T') {
int u;
RD(u);
if(mp.count(u) == 0) {
puts("invalid priority.");
continue;
}
u = mp[u];
int pos = -1;
REP(i, top) if(que[i] == u) pos = i;
if(pos == -1) {
puts("invalid priority.");
continue;
}
puts("success.");
aws = u;
} else if( op[0] == 'U') {
if(aws == 0) {
puts("no such person.");
continue;
}
puts("success.");
aws = 0;
} else if( op[0] == 'C') {
int len = strlen(op);
if(len == 5) {
int u;
RD(u);
if(mp.count(u) == 0) {
puts("invalid priority.");
continue;
}
u = mp[u];
int pos = -1;
REP(i, top) if(que[i] == u) pos = i;
if(pos == -1) {
puts("invalid priority.");
continue;
}
if( u == aws) aws = 0;
printf("close %d with %lld.\n",ache[u], cnt[u]);
cnt[u] = 0;
del(pos);
}else if(len == 4) {
int c;
RD(c);
if( top == 0 ) {
puts("empty.");
continue;
}
puts("success.");
if( aws ) cnt[aws] += c;
else cnt[que[1]] += c;
}else if( len == 6) {
int u;
RD(u);
if(mp.count(u) == 0) {
puts("invalid priority.");
continue;
}
u = mp[u];
int pos = -1;
REP(i, top) if( que[i] == u ) pos = i;
if( pos == -1) {
puts("invalid priority.");
continue;
}
puts("success.");
gotop(pos);
}
}
}
if( aws && cnt[aws]) {
printf("Bye %d: %lld\n", ache[aws], cnt[aws]);
int pos = -1;
REP(i, top) if(que[i] == aws) pos = i;
del(pos);
}
REP(i, top) {
if( cnt[que[i]] ) printf("Bye %d: %lld\n", ache[que[i]], cnt[que[i]]);
}
}
}