hdu 5071 Chat (鞍山现场赛B题)

这道题目是叙述的比较多,但是还是一道容易题,没有什么复杂的算法,考察的是基本功,还有就是细心程度,如果不细心很容易出错的。我们当时就是因为漏掉了最后一段话中的条件,一直错,后来比赛完才发现。
题目需要注意的地方是,那个特殊状态,always on top,那仅仅是一个状态,并不影响它在队列中的位置,还有就是当前顶部的女孩,如果有特殊状态的女孩,就是这个女孩,否则,就是队列头的女孩。
使用普通的结构体数组就可以模拟出来,存三个变量,即优先级,说的话数,是不是有特殊标记,要注意,应该是会爆int 的,要使用long long。
最后一段话中包含了很多信息,最后说Bye的时候,没有说话的是不说Bye的,说Bye的时候也先要和当前顶部女孩说Bye,当关闭某个窗口是,如果当前窗口的女孩是当前顶部的女孩也要说Bye,处理好这几个细节,其他的地方细心一下,应该就没有问题了。
代码如下:
/*************************************************************************
	> File Name: b.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年10月22日 星期三 12时30分20秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define SQR(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
#define middle(x, y) ((x + y) >> 1)

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 5050

struct Node {
	long long u, c, istop;
}node[N];

int top;
char type[N];

int main(int argc, char *argv[])
{
	int t, n;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);

		top = 0;
		for (int cs = 1; cs <= n; ++cs) {
			long long u, x, w, flag, pos;

			scanf("%s", type);
			printf("Operation #%d: ", cs);
			if (strcmp(type, "Add") == 0) {
				cin >> u;

				flag = 0;
				for (int i = 0; i < top; ++i) {
					if (node[i].u == u) {
						flag = 1;
						break;
					}
				}

				if (flag == 0) {
					node[top].u = u;
					node[top].c = 0;
					node[top].istop = 0;
					++top;
					printf("success.\n");
				} else {
					printf("same priority.\n");
				}
			} else if (strcmp(type, "Close") == 0) {
				cin >> u;

				pos = 0;
				flag = 0;
				for (int i = 0; i < top; ++i) {
					if (node[i].u == u) {
						flag = 1;
						pos = i;
						break;
					}
				}

				if (flag == 0) {
					printf("invalid priority.\n");
				} else {
					//关闭前,如果是当前顶部女孩要说Bye
					int tmp = 0;
					for (int i = 1; i < top; ++i) {
						if (node[i].istop == 1) {
							tmp = 1;
							break;
						}
					}
					if (node[pos].istop == 1 || pos == 0) {
						if (node[pos].c > 0) {
							cout << "Bye " << node[pos].u << ": " << node[pos].c << endl;
						}
					}

					cout << "close " << node[pos].u << " with " << node[pos].c << "." << endl;
					for (int i = pos + 1; i < top; ++i) {
						node[i - 1] = node[i];
					}
					--top;
				}
			} else if (strcmp(type, "Chat") == 0) {
				cin >> w;

				pos = 0;
				flag = 0;
				for (int i = 0; i < top; ++i) {
					if (node[i].istop == 1) {
						flag = 1;
						pos = i;
						break;
					}
				}

				if (top == 0) {
					printf("empty.\n");
				} else {
					printf("success.\n");
					if (flag == 0) {
						node[0].c += w;
					} else {
						node[pos].c += w;
					}
				}
			} else if (strcmp(type, "Rotate") == 0) {
				cin >> x;

				--x;
				flag = 0;
				pos = 0;

				if (x < 0 || x >= top) {
					printf("out of range.\n");
				} else {
					printf("success.\n");

					Node tmp = node[x];
					for (int i = x - 1; i >= 0; --i) {
						node[i + 1] = node[i];
					}
					node[0] = tmp;
				}
			} else if (strcmp(type, "Prior") == 0) {
				if (top == 0) {
					printf("empty.\n");
				} else {
					printf("success.\n");

					pos = 0;
					for (int i = 0; i < top; ++i) {
						if (node[pos].u < node[i].u) {
							pos = i;
						}
					}

					Node tmp = node[pos];
					for (int i = pos - 1; i >= 0; --i) {
						node[i + 1] = node[i];
					}
					node[0] = tmp;
				}
			} else if (strcmp(type, "Choose") == 0) {
				cin >> u;

				pos = 0;
				flag = 0;
				for (int i = 0; i < top; ++i) {
					if (node[i].u == u) {
						flag = 1;
						pos = i;
						break;
					}
				}

				if (flag == 0) {
					printf("invalid priority.\n");
				} else {
					printf("success.\n");

					Node tmp = node[pos];
					for (int i = pos - 1; i >= 0; --i) {
						node[i + 1] = node[i];
					}
					node[0] = tmp;
				}
			} else if (strcmp(type, "Top") == 0) {
				cin >> u;

				pos = 0;
				flag = 0;
				for (int i = 0; i < top; ++i) {
					if (node[i].u == u) {
						flag = 1;
						pos = i;
						break;
					}
				}

				if (flag == 0) {
					printf("invalid priority.\n");
				} else {
					printf("success.\n");

					for (int i = 0; i < top; ++i) {
						node[i].istop = 0;
					}
					node[pos].istop = 1;
				}
			} else if (strcmp(type, "Untop") == 0) {
				flag = 0;

				for (int i = 0; i < top; ++i) {
					if (node[i].istop == 1) {
						flag = 1;
						node[i].istop = 0;
						break;
					}
				}

				if (flag == 0) {
					printf("no such person.\n");
				} else {
					printf("success.\n");
				}
			}
		}
		for (int i = 0; i < top; ++i) {
			if (node[i].istop == 1 && node[i].c > 0) {
				cout << "Bye " << node[i].u << ": " << node[i].c << endl;
			}
		}
		for (int i = 0; i < top; ++i) {
			if (node[i].istop != 1 && node[i].c > 0) {
				cout << "Bye " << node[i].u << ": " << node[i].c << endl;
			}
		}
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值