HDU 5071 Chat (模拟)

题目: 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 1000000000
typedef __int64 LL; 
#define N 5011
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值