HDU 5071 Chat (模拟)

本文详细解读了一个涉及队列操作、优先级管理与数据结构应用的算法问题,通过实例展示了如何使用vector、map等数据结构解决实际问题,并提供了完整的代码实现。在模拟赛中,这一问题的解决激发了参赛者的热情。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

题意还是很明确的, 就是最后一段要仔细读

分析:

需要的注意的就是top操作不是真的放到队列首, 只是一个标记, 最后Bye的时候要先给top的, 然后没有的话, 就从头开始Bye
vector,map,Closemap
1Y

代码:

//
//  Created by TaoSama on 2015-10-10
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n;
vector<int> v;
map<int, int> mp;

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

//  vector<int> vv; vv.push_back(1); vv.push_back(2);
//  vv.insert(vv.begin(), 3);
//  for(auto v : vv) printf("%d ", v); puts("");

    int t; scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        char op[10]; int x, top = -1;
        v.clear(); mp.clear();
        for(int i = 1; i <= n; ++i) {
            scanf("%s", op);
            printf("Operation #%d: ", i);
            if(!strcmp(op, "Add")) {
                scanf("%d", &x);
                if(mp.count(x)) puts("same priority.");
                else {
                    v.push_back(x);
                    mp[x] = 0;
                    puts("success.");
                }
            } else if(!strcmp(op, "Close")) {
                scanf("%d", &x);
                if(!mp.count(x)) puts("invalid priority.");
                else {
                    v.erase(find(v.begin(), v.end(), x));
                    printf("close %d with %d.\n", x, mp[x]);
                    mp.erase(x);
                }
            } else if(!strcmp(op, "Chat")) {
                scanf("%d", &x);
                if(!v.size()) puts("empty.");
                else {
                    if(~top) mp[top] += x;
                    else mp[v[0]] += x;
                    puts("success.");
                }
            } else if(!strcmp(op, "Rotate")) {
                scanf("%d", &x);
                if(x < 0 || x > v.size()) puts("out of range.");
                else {
                    int tmp = v[x - 1];
                    v.erase(v.begin() + x - 1);
                    v.insert(v.begin(), tmp);
                    puts("success.");
                }
            } else if(!strcmp(op, "Prior")) {
                if(!v.size()) puts("empty.");
                else {
                    int idx = 0;
                    for(int k = 0; k < v.size(); ++k)
                        if(v[k] > v[idx]) idx = k;
                    int tmp = v[idx];
                    v.erase(v.begin() + idx);
                    v.insert(v.begin(), tmp);
                    puts("success.");
                }
            } else if(!strcmp(op, "Choose")) {
                scanf("%d", &x);
                if(!mp.count(x)) puts("invalid priority.");
                else {
                    v.erase(find(v.begin(), v.end(), x));
                    v.insert(v.begin(), x);
                    puts("success.");
                }
            } else if(!strcmp(op, "Top")) {
                scanf("%d", &x);
                if(!mp.count(x)) puts("invalid priority.");
                else {
                    top = x;
                    puts("success.");
                }
            } else {
                if(~top) {
                    top = -1;
                    puts("success.");
                } else puts("no such person.");
            }
        }
        int tmp;
        if(~top) {
            tmp = mp[top];
            if(tmp) printf("Bye %d: %d\n", top, tmp);
        }
        for(int k = 0; k < v.size(); ++k) {
            if(v[k] == top) continue;
            tmp = mp[v[k]];
            if(tmp) printf("Bye %d: %d\n", v[k], tmp);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值