HDU 5071 Chat(大模擬)


是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]]);
		}
	}
}


發佈了308 篇原創文章 · 獲贊 11 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章