【模拟】 HDOJ 5071 Chat

有很多坑的模拟题。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 50005
#define maxm 200005
#define eps 1e-10
#define mod 1000000007
#define INF 1e17
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head
//4897

struct node
{
	int u;
	LL w;
	int next, pre;
}pool[maxn];
int z, root, tail;
set<int> ss;
char s[maxn];
int Top, size;
map<int, int> mpp;

void init(void)
{
	mpp.clear();
	ss.clear();
	z = Top = size = 0;
	root = tail = ++z;
	pool[root].u = pool[root].w = 0;
	pool[root].next = pool[root].pre = 0;
}

void debug(void)
{
	for(int i = root; i; i = pool[i].next) {
		printf("u = %d w = %lld\n", pool[i].u, pool[i].w);
	}
	printf("\n");
}

void add(int u)
{
	if(mpp.count(u)) {
		printf("same priority.\n");
		return;
	}
	printf("success.\n");
	size++;
	ss.insert(u);
	mpp[u] = ++z;
	pool[z].u = u;
	pool[z].w = 0;
	pool[z].pre = tail;
	pool[z].next = 0;
	pool[tail].next = z;
	tail = z; 
}

void close(int u)
{
	if(!mpp.count(u)) {
		printf("invalid priority.\n");
		return;
	}
	
	int t = mpp[u];
	mpp.erase(u);
	ss.erase(u);
	if(Top == t) Top = 0;
	size--;
	printf("close %d with %I64d.\n", u, pool[t].w);
	if(t == tail) tail = pool[t].pre;
	pool[pool[t].pre].next = pool[t].next;
	pool[pool[t].next].pre = pool[t].pre;
}

void chat(int w)
{
	if(root == tail) {
		printf("empty.\n");
		return;
	}
	
	printf("success.\n");
	if(Top == 0) pool[pool[root].next].w += w;
	else pool[Top].w += w;
}

void rotate(int x)
{
	if(size < x || x < 1) {
		printf("out of range.\n");
		return;
	}
	printf("success.\n");
	int now = root;
	for(int i = 1; i <= x; i++) now = pool[now].next;
	if(now == tail) tail = pool[now].pre;
	pool[pool[now].pre].next = pool[now].next;
	pool[pool[now].next].pre = pool[now].pre;
	pool[now].pre = root;
	pool[now].next = pool[root].next;
	pool[pool[root].next].pre = now;
	pool[root].next = now;
	
}

void choose(int u)
{
	if(!mpp.count(u)) {
		printf("invalid priority.\n");
		return;
	}
	printf("success.\n");
	int t = mpp[u];
	if(t == tail) tail = pool[t].pre;
	pool[pool[t].pre].next = pool[t].next;
	pool[pool[t].next].pre = pool[t].pre;
	pool[t].pre = root;
	pool[t].next = pool[root].next;
	pool[pool[root].next].pre = t;
	pool[root].next = t;
}

void prior(void)
{
	if(size == 0) {
		printf("empty.\n");
		return;
	}
	int t = *(--ss.end());
	//set<int>::iterator it;
	//for(it = ss.begin(); it != ss.end(); it++) printf("AAA %d\n", *it);
	choose(t);
}

void top(int u)
{
	if(ss.find(u) == ss.end()) {
		printf("invalid priority.\n");
		return;
	}
	printf("success.\n");
	Top = mpp[u];
}

void untop(void)
{
	if(Top == 0) {
		printf("no such person.\n");
		return;
	}
	printf("success.\n");
	Top = 0;
}

void bye(void)
{
	if(!Top) {
		for(int i = pool[root].next; i; i = pool[i].next) {
			if(pool[i].w) 
				printf("Bye %d: %I64d\n", pool[i].u, pool[i].w);
		}
	}
	else {
		if(pool[Top].w) printf("Bye %d: %I64d\n", pool[Top].u, pool[Top].w);
		for(int i = pool[root].next; i; i = pool[i].next) {
			if(i == Top) continue;
			if(pool[i].w) printf("Bye %d: %I64d\n", pool[i].u, pool[i].w);
		}
	}
}

void work(void)
{
	int n, x;
	scanf("%d", &n);
	//debug();
	for(int i = 1; i <= n; i++) {
		scanf("%s", s);
		printf("Operation #%d: ", i);
		if(strcmp(s, "Add") == 0) scanf("%d", &x), add(x);
		else if(strcmp(s, "Prior") == 0) prior();
		else if(strcmp(s, "Close") == 0) scanf("%d", &x), close(x);
		else if(strcmp(s, "Chat") == 0) scanf("%d", &x), chat(x);
		else if(strcmp(s, "Rotate") == 0) scanf("%d", &x), rotate(x);
		else if(strcmp(s, "Choose") == 0) scanf("%d", &x), choose(x);
		else if(strcmp(s, "Top") == 0) scanf("%d", &x), top(x);
		else if(strcmp(s, "Untop") == 0) untop();
		//debug();
	}
	bye();
}

int main(void)
{
	int _;
	while(scanf("%d", &_)!=EOF) {
		while(_--) {
			init();
			work();
		}
	}

	return 0;
}


你可能感兴趣的:(HDU)