注意:“always on the top”是一种属性,在节点里标记一下就可以。
写得有点长,见谅。。。
//Chat.cpp
#include <ios>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <cstdio>
#include <cwchar>
#include <iosfwd>
#include <limits>
#include <locale>
#include <memory>
#include <string>
#include <vector>
#include <cassert>
#include <ciso646>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <istream>
#include <numeric>
#include <ostream>
#include <sstream>
#include <utility>
#include <iostream>
#include <iterator>
#include <valarray>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <streambuf>
#include <functional>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define DEBUG puts("Here is a BUG");
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)1e9 + 5;
using namespace std;
const string empty = "empty.";
const string success = "success.";
const string OutOfRange = "out of range.";
const string same_priority = "same priority.";
const string no_such_person = "no such person.";
const string invalid_priority = "invalid priority.";
struct Node {
int u;
LL c;
bool isTop;
Node() { u = -1; c = 0; isTop = false; }
Node(int u): u(u) { c = 0; isTop = false; }
Node(int u, int c): u(u), c(c) { isTop = false; }
};
bool isExistTop;
list<Node> clj;
list<Node>::iterator top, it;
inline void init(void) {
isExistTop = false;
clj.clear();
top = clj.end();
}
inline void CLOSE(int u, LL c) {
cout << "close " << u << " with " << c << "." << endl;
}
inline int get_command(void) {
char order[10];
cin >> order;
if (order[0] == 'A') return 1;
if (order[0] == 'C') {
if (order[1] == 'l') return 2;
if (order[2] == 'a') return 3;
return 6;
}
if (order[0] == 'R') return 4;
if (order[0] == 'P') return 5;
if (order[0] == 'T') return 7;
if (order[0] == 'U') return 8;
}
inline void Add(int u) {
for(it = clj.begin(); it != clj.end(); it++) {
if (it->u == u) {
cout << same_priority << endl;
return;
}
}
clj.push_back(Node(u));
cout << success << endl;
}
inline void Close(int u) {
for(it = clj.begin(); it != clj.end(); it++) {
if (it->u == u) {
CLOSE(u, it->c);
if (it->isTop) {
top = clj.end();
isExistTop = false;
}
it->isTop = false; it->u = -1; it->c = 0;
clj.erase(it);
return;
}
}
cout << invalid_priority << endl;
}
inline void Chat(int w) {
if (isExistTop) {
top->c += w;
cout << success << endl;
return;
}
if (clj.empty()) {
cout << empty << endl;
return;
}
clj.begin()->c += w;
cout << success << endl;
return;
}
inline void Rotate(int x) {
if (x < 1 || x > clj.size()) {
cout << OutOfRange << endl;
return;
}
it = clj.begin();
while(--x) it++;
clj.push_front(*it);
if (it->isTop) {
top = clj.begin();
}
clj.erase(it);
cout << success << endl;
return;
}
inline void Prior(int u) {
if (clj.empty()) {
cout << empty << endl;
return;
}
bool flag = false;
list<Node>::iterator tmp = clj.begin(); it = tmp;
for(it++; it != clj.end(); it++) {
if (it->u > tmp->u) {
tmp = it;
}
}
Node t = (*tmp);
clj.erase(tmp);
clj.push_front(t);
if (t.isTop) top = clj.begin();
cout << success << endl;
}
inline void Choose(int u) {
for(it = clj.begin(); it != clj.end(); it++) {
if (it->u == u) {
clj.push_front(*it);
if (it->isTop) {
top = clj.begin();
}
clj.erase(it);
cout << success << endl;
return;
}
}
cout << invalid_priority << endl;
}
inline void Top(int u) {
if (isExistTop && top->u == u) {
cout << success << endl;
return;
}
if (isExistTop) {
for(it = clj.begin(); it != clj.end(); it++) {
if (it->u == u) {
it->isTop = true;
top->isTop = false;
top = it;
cout << success << endl;
return;
}
}
cout << invalid_priority << endl;
return;
}
for(it = clj.begin(); it != clj.end(); it++) {
if (it->u == u) {
top = it;
isExistTop = it->isTop = true;
cout << success << endl;
return;
}
}
cout << invalid_priority << endl;
return;
}
inline void Untop(int u) {
if (isExistTop) {
top->isTop = isExistTop = false;
top = clj.end();
cout << success << endl;
return;
}
top = clj.end();
cout << no_such_person << endl;
return;
}
inline void say_Bye(int u, LL c) {
cout << "Bye " << u << ": " << c << endl;
return;
}
inline void Bye(void) {
if (isExistTop) {
if (top->c) say_Bye(top->u, top->c);
clj.erase(top);
top = clj.end();
isExistTop = false;
}
for(it = clj.begin(); it != clj.end(); it++) {
if (it->c) {
say_Bye(it->u, it->c);
}
}
return;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
void (*work[])(int) = {Add, Close, Chat, Rotate, Prior, Choose, Top, Untop};
int T; cin >> T;
while(T--) {
init();
int n; cin >> n;
for(int kase = 1; kase <= n; kase++) {
int u;
int op = get_command();
if (op-5 && op-8) cin >> u;
cout << "Operation #" << kase << ": ";
(*work[op-1])(u);
}
Bye();
}
return 0;
}