hdu 5071(2014鞍山现场赛B题,大模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071
思路:模拟题,没啥可说的,移动的时候需要注意top的变化。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = (5000 + 500); struct Girl { int priority; __int64 words; Girl() {} Girl(int _priority, __int64 _words) : priority(_priority), words(_words) {} }; struct Queue { int len, top; struct Girl girl[MAX_N]; void Init() { len = 0; top = -1; } void Add(int u) { int tag = 0; for (int i = 0; i < len; ++i) { if (girl[i].priority == u) { tag = 1; break; } } if (tag) { puts("same priority."); return; } girl[len++] = Girl(u, 0); puts("success."); } void Close(int u) { int pos = -1; for (int i = 0; i < len; ++i) { if (girl[i].priority == u) { pos = i; break; } } if (pos == -1) { puts("invalid priority."); return; } printf("close %d with %I64d.\n", girl[pos].priority, girl[pos].words); if (top != -1) { if (top == pos) top = -1; else if (top > pos) top--; } for (int i = pos + 1; i < len; ++i) { girl[i - 1] = girl[i]; } --len; } void Chat(int w) { if (len == 0) { puts("empty."); return; } puts("success."); if (top == -1) { girl[0].words += (__int64)w; } else girl[top].words += (__int64)w; } void Rotate(int x) { if (x < 0 || x >= len) { puts("out of range."); return; } puts("success."); if (top != -1) { if (top == x) top = 0; else if (top < x) top++; } Girl tmp = girl[x]; for (int i = x; i > 0; --i) girl[i] = girl[i - 1]; girl[0] = tmp; } void Prior() { if (len == 0) { puts("empty."); return; } puts("success."); int max_priority = -1, pos = -1; for (int i = 0; i < len; ++i) { if (girl[i].priority > max_priority) max_priority = girl[i].priority, pos = i; } if (pos > 0) { if (top != -1) { if (top == pos) top = 0; else if (top < pos) top++; } Girl tmp = girl[pos]; for (int i = pos; i > 0; --i) { girl[i] = girl[i - 1]; } girl[0] = tmp; } } void Choose(int u) { int pos = -1; for (int i = 0; i < len; ++i) { if (girl[i].priority == u) { pos = i; break; } } if (pos == -1) { puts("invalid priority."); return; } puts("success."); if (top != -1) { if (top == pos) top = 0; else if (top < pos) top++; } Girl tmp = girl[pos]; for (int i = pos; i > 0; --i) girl[i] = girl[i - 1]; girl[0] = tmp; } void Top(int u) { int pos = -1; for (int i = 0; i < len; ++i) { if (girl[i].priority == u) { pos = i; break; } } if (pos == -1) { puts("invalid priority."); return; } puts("success."); top = pos; } void Untop() { if (top == -1) { puts("no such person."); return; } puts("success."); top = -1; } void Bye() { if (top != -1 && girl[top].words > 0) { printf("Bye %d: %I64d\n", girl[top].priority, girl[top].words); } for (int i = 0; i < len; ++i) { if (i != top && girl[i].words > 0) { printf("Bye %d: %I64d\n", girl[i].priority, girl[i].words); } } } } que; int N, u, w, x; char str[22]; int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d", &N); que.Init(); for (int i = 1; i <= N; ++i) { scanf("%s", str); printf("Operation #%d: ", i); if (strcmp(str, "Add") == 0) { scanf("%d", &u); que.Add(u); } else if (strcmp(str, "Close") == 0) { scanf("%d", &u); que.Close(u); } else if (strcmp(str, "Chat") == 0) { scanf("%d", &w); que.Chat(w); } else if (strcmp(str, "Rotate") == 0) { scanf("%d", &x); que.Rotate(x - 1); } else if (strcmp(str, "Prior") == 0) { que.Prior(); } else if (strcmp(str, "Choose") == 0) { scanf("%d", &u); que.Choose(u); } else if (strcmp(str, "Top") == 0) { scanf("%d", &u); que.Top(u); } else if (strcmp(str, "Untop") == 0) { que.Untop(); } } que.Bye(); } return 0; }