zoukankan      html  css  js  c++  java
  • hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat

    题目大意:模拟题。

    。。

    注意最后说bye的时候仅仅要和讲过话的妹子说再见。

    解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子。数组A和N记录等级的顺序,添加

    删除等操作全然能够同过数组上的模拟,时间足够。

    T和flag标记是否有置顶窗体。

    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 5005;
    typedef long long ll;
    
    map<int, ll> C;
    int x, N, A[maxn], T, flag;
    vector<int> vec;
    
    int find(int a) {
        for (int i = 0; i < N; i++)
            if (A[i] == a)
                return i;
        return N;
    }
    
    void add ()  {
        scanf("%d", &x);
        if (C.count(x))
            printf("same priority");
        else {
            A[N++] = x;
            C[x] = 0;
            printf("success");
        }
    }
    
    void close() { 
        scanf("%d", &x);
        if (C.count(x)) {
            printf("close %d with %I64d", x, C[x]);
            C.erase(x);
    
            if (flag && T == x)
                flag = 0;
    
            int pos = find(x);
            for (int i = pos; i < N; i++)
                A[i] = A[i+1];
            N--;
        } else
            printf("invalid priority");
    }
    
    void chat() {
        scanf("%d", &x);
        if (N == 0)
            printf("empty");
        else {
            if (flag) {
                C[T] += x;
                vec.push_back(T);
            } else {
                C[A[0]] += x;
                vec.push_back(A[0]);
            }
            printf("success");
        }
    }
    
    void rotate() {
        scanf("%d", &x);
        if (x > N || x < 1)
            printf("out of range");
        else {
            int tmp = A[x-1];
            for (int i = x-1; i; i--)
                A[i] = A[i-1];
            A[0] = tmp;
            printf("success");
        }
    }
    
    void prior() {
        if (N == 0)
            printf("empty");
        else {
            int x = A[0], pos = 0;
            for (int i = 1; i < N; i++) {
                if (A[i] > x) {
                    x = A[i];
                    pos = i;
                }
            }
    
            int tmp = A[pos];
            for (int i = pos; i; i--)
                A[i] = A[i-1];
            A[0] = tmp;
            printf("success");
        }
    }
    
    void choose() {
        scanf("%d", &x);
        if (C.count(x)) {
            int pos = find(x);
    
            int tmp = A[pos];
            for (int i = pos; i; i--)
                A[i] = A[i-1];
            A[0] = tmp;
            printf("success");
        } else
            printf("invalid priority");
    }
    
    void top() {
        scanf("%d", &x);
        if (C.count(x)) {
            T = x;
            flag = 1;
            printf("success");
        } else
            printf("invalid priority");
    }
    
    void untop() {
        if (flag) {
            flag = 0;
            printf("success");
        } else 
            printf("no such person");
    }
    
    void solve() {
        if (N == 0)
            return;
    
        if (flag && C[T])
            printf("Bye %d: %I64d
    ", T, C[T]);
    
        for (int i = 0; i < N; i++) {
            if (flag && A[i] == T)
                continue;
            if (C[A[i]])
                printf("Bye %d: %I64d
    ", A[i], C[A[i]]);
        }
    
        /*
           int t = flag ?

    T : A[0]; if (C[t]) printf("Bye %d: %I64d ", t, C[t]); for (int i = vec.size() - 1; i >= 0; i--) { if (C.count(vec[i]) && t != vec[i]) { printf("Bye %d: %I64d ", vec[i], C[vec[i]]); break; } } */ } int main () { int cas; scanf("%d", &cas); while (cas--) { N = T = flag = 0; vec.clear(); C.clear(); int Q; char op[10]; scanf("%d", &Q); for (int i = 1; i <= Q; i++) { scanf("%s", op); printf("Operation #%d: ", i); if (strcmp(op, "Add") == 0) add(); else if (strcmp(op, "Close") == 0) close(); else if (strcmp(op, "Chat") == 0) chat(); else if (strcmp(op, "Rotate") == 0) rotate(); else if (strcmp(op, "Prior") == 0) prior(); else if (strcmp(op, "Choose") == 0) choose(); else if (strcmp(op, "Top") == 0) top(); else if (strcmp(op, "Untop") == 0) untop(); printf(". "); } solve(); } return 0; }

  • 相关阅读:
    微信公众平台—— 获取微信服务器IP地址
    微信公众平台——获取access_token、expires_in
    PHP版本切换
    开源各种系统
    VUE -- 如何快速的写出一个Vue的icon组件?
    Mac下php 5升级到php 7的步骤详解
    Nginx反向代理导致PHP获取不到正确的HTTP_HOST,SERVER_NAME,客户端IP的解决方法
    nginx服务器URL无法自动添加index.php
    php类库安装xml simplexml
    Mac DBeaver Client home is not specified for connection解决办法
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5088225.html
Copyright © 2011-2022 走看看