HDU 5071 Chat (模拟)

来源:互联网 发布:古筝陪练软件 编辑:程序博客网 时间:2024/06/14 09:30

题目: LINK

题目so long 的模拟题, 几种操作不难, 要理解“always on top ”这个特殊状态它的位置不变,注意 “Bye u: c”这个操作时, 只输出说过话的人, 而且优先输出“always on top ”的。

竟然没有看到输出后面有".", wa了好多发


#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <cmath>#include <queue>#include <map>#include <set>using namespace std; #define INF 1000000000typedef __int64 LL; #define N 5011int t, n; struct node {    int pri;     LL sum; }p[N]; map<int , int> M; int top, len; char ope[20]; void add(int x) {    if(M[x]) {        puts("same priority."); return ;     }    len ++;     M[x] = len;     p[len].pri = x; p[len].sum = 0;     puts("success."); }void close(int x) {    if(!M[x]) {        puts("invalid priority."); return ;     }    int t = M[x];     printf("close %d with %I64d.\n", x, p[t].sum);     M[x] = 0;     for(int i = t; i < len; i++) {        p[i] = p[i+1]; M[p[i].pri] = i;     }    len --; }void chat(int x) {    if(!len) {        puts("empty."); return ;     }    puts("success.");     if(top != -1) {        int t = M[top];         p[t].sum += (LL)x;     }    else  p[1].sum += (LL)x; }void rotate(int x) {    if(x < 1 || x > len) {        puts("out of range."); return ;     }    puts("success.");     node tmp = p[x];     for(int i = x; i >= 2; i --) {        p[i] = p[i-1]; M[p[i].pri] = i;     }    p[1] = tmp;     M[p[1].pri] = 1; }void prior() {    if(len <= 0) {        puts("empty."); return ;     }    map<int, int> ::iterator it;     it = M.end(); it --;     rotate(it->second); }void choose(int x) {    if(!M[x]) {        puts("invalid priority."); return ;     }    rotate(M[x]); }void Top(int x) {    if(!M[x]) {        puts("invalid priority."); return ;     }    puts("success.");     top = x; }void untop() {    if(top == -1) {        puts("no such person."); return ;     }    puts("success.");     top = -1; }void bye() {    if(top != -1) {        int t = M[top];         if(p[t].sum != 0) printf("Bye %d: %I64d\n", top, p[t].sum);     }    for(int i = 1; i<= len; i ++) {        if(p[i].pri == top) continue;          if(p[i].sum == 0) continue;         printf("Bye %d: %I64d\n", p[i].pri, p[i].sum);     }}int main() {    scanf("%d", &t);     while(t--) {        scanf("%d", &n);         top = -1;         len = 0;         M.clear();         int x;         for(int i = 1; i <= n; i++) {            scanf("%s", ope);             printf("Operation #%d: ", i);             if(strcmp(ope, "Add") == 0) {                scanf("%d", &x);                 add(x);             }            else if(strcmp(ope, "Close") == 0) {                scanf("%d", &x);                 close(x);             }            else if(strcmp(ope, "Chat") == 0) {                scanf("%d", &x);                 chat(x);             }            else if(strcmp(ope, "Rotate") == 0) {                scanf("%d", &x);                 rotate(x);             }            else if(strcmp(ope, "Prior") == 0) {                prior();             }            else if(strcmp(ope, "Choose") == 0) {                scanf("%d", &x);                 choose(x);             }            else if(strcmp(ope, "Top") == 0) {                scanf("%d", &x);                 Top(x);             }            else if(strcmp(ope, "Untop") == 0) {                untop();             }        }        bye();     }    return 0; }


0 0
原创粉丝点击