hdu 5071 Chat (模拟题)

题目很长,但理解清楚就很好做了。

有两个值得注意的地方:
* 最后输出Bye的时候每次只输出当前的top。所以一开始若有Always on top,就要首先输出。
* 再输出Bye的时候,若是一句话也没讲,就不用输出

代码

#include <iostream>
#include <string>
#include <map>
#include <vector>

using namespace std;
vector<long long> V;
map<long long,long long> mp;
long long firFlag;

void init()
{
    firFlag = 0;
    mp.clear();
    V.clear();
}
void Add(long long cnt)
{
    long long x; cin >> x;
    cout << "Operation #" << cnt << ": ";
    if(mp.count(x)) cout << "same priority." << endl;
    else {
        V.push_back(x);
        mp[x] = 0;
        cout << "success." << endl;
    }
}
void Close(long long cnt)
{
    long long x; cin >> x;
    cout << "Operation #" << cnt << ": ";
    if(mp.count(x)) {
        cout << "close " << x << " with " << mp[x] << "." << endl;
        //mp.erase(x);
        map<long long,long long>::iterator iter = mp.begin();
        for(; iter != mp.end(); iter++) if(iter->first == x) break;
        mp.erase(iter);

        vector<long long>::iterator it;
        for(it = V.begin(); it != V.end(); it++) if(*it == x) break;
        V.erase(it);
        if(firFlag == x) firFlag = 0;
    }
    else cout << "invalid priority." << endl;
}
void Chat(long long cnt)
{
    long long x; cin >> x;
    cout << "Operation #" << cnt << ": ";
    if(V.empty()) cout << "empty." << endl;
    else {
        cout << "success." << endl;
        if(firFlag) mp[firFlag] += x;
        else {
            long long tmp = V.front();
            mp[tmp] += x;
        }
    }
}
void Rotate(long long cnt)
{
    long long x; cin >> x;
    cout << "Operation #" << cnt << ": ";
    long long m = V.size();
    if(x < 1 || x > m) cout << "out of range." << endl;
    else {
        cout << "success." << endl;
        long long tmp = V[x-1];
        vector<long long>::iterator it = V.begin();
        for(long long k = 1; k < x; k++) it++;
        V.erase(it);
        V.insert(V.begin(),tmp);
    }
}
void Prior(long long cnt)
{
    cout << "Operation #" << cnt << ": ";
    if(V.empty()) cout << "empty." << endl;
    else {
        cout << "success." << endl;
        map<long long,long long>::iterator iter = mp.end();
        --iter;
        long long x = iter->first;

        vector<long long>::iterator it;
        for(it = V.begin(); it != V.end(); it++) if(*it == x) break;
        V.erase(it);
        V.insert(V.begin(), x);
    }
}
void Choose(long long cnt)
{
    long long x; cin >> x;
    cout << "Operation #" << cnt << ": ";
    if(mp.count(x)) {
        cout << "success." << endl;
        vector<long long>::iterator it;
        for(it = V.begin(); it != V.end(); it++) if(*it == x) break;
        V.erase(it);
        V.insert(V.begin(), x);
    }
    else cout << "invalid priority." << endl;
}
void Top(long long cnt)
{
    long long x; cin >> x;
    cout << "Operation #" << cnt << ": ";
    if(mp.count(x)) {
        firFlag = x;
        cout << "success." << endl;
    }
    else cout << "invalid priority." << endl;
}
void Untop(long long cnt)
{
    cout << "Operation #" << cnt << ": ";
    if(firFlag) {
        firFlag = 0;
        cout << "success." << endl;
    }
    else cout << "no such person." << endl;
}

int main()
{
    long long t , n;
    cin >> t;
    while(t--)
    {
        init();
        cin >> n;
        for(long long i = 1; i <= n; i++) {
            string s; cin >> s;
            if(s == "Add") Add(i);
            else if(s == "Close") Close(i);
            else if(s == "Chat") Chat(i);
            else if(s == "Rotate") Rotate(i);
            else if(s == "Prior") Prior(i);
            else if(s == "Choose") Choose(i);
            else if(s == "Top") Top(i);
            else if(s == "Untop") Untop(i);
        }
        if(!V.empty())
        {
            if(firFlag && mp[firFlag]) cout << "Bye " << firFlag << ": " << mp[firFlag] << endl;
            for(unsigned i = 0; i < V.size(); i++) {
                if(V[i] != firFlag && mp[V[i]]) {
                    cout << "Bye " << V[i] << ": " << mp[V[i]] << endl;
                }

            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值