210 - Concurrency Simulator (UVA)

题目链接如下:

Online Judge

题目中给出的测试样例是错的,最前面需要给出case个数。

代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <cstdlib>
// #define debug

int n, m, quant, kase;
int t[5], val[26], ip[11];
std::string str;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &n);
    for (kase = 0; kase < n; ++kase){
        std::deque<int> readyQ, blockedQ;
        bool flag = true;
        for (int i = 0; i < 26; ++i){
            val[i] = 0;
        }
        for (int i = 0; i < 11; ++i){
            ip[i] = 0;
        }
        printf("%s", kase ? "\n" : "");
        scanf("%d", &m);
        std::vector<std::vector<std::string> > lineVec(m + 1);
        for (int i = 0; i < 5; ++i){
            scanf("%d", &t[i]);
        }
        scanf("%d\n", &quant);
        int pro = 1;
        while (getline(std::cin, str)){
            lineVec[pro].push_back(str);
            if (str == "end"){
                pro++;
                if (pro == m + 1){
                    break;
                }
            }
        }
        for (int i = 1; i <= m; ++i){
            readyQ.push_back(i);
        }
        while (1){
            if (readyQ.empty()){
                break;
            }
            int temp = readyQ.front();
            readyQ.pop_front();
            int tt = quant;
            bool ff = true;
            for (; tt > 0 && ip[temp] < lineVec[temp].size(); ip[temp]++){
                std::string ss = lineVec[temp][ip[temp]];
                if (ss[0] == 'p' && ss[1] == 'r'){
                    printf("%d: %d\n", temp, val[ss.back() - 'a']);
                    tt -= t[1];
                } else if (ss[0] == 'l' && ss[1] == 'o'){
                    if (!flag){
                        blockedQ.push_back(temp);
                        ff = false;
                        break;
                    }
                    flag = false;
                    tt -= t[2];
                } else if (ss[0] == 'u' && ss[1] == 'n'){
                    tt -= t[3];
                    flag = true;
                    if (!blockedQ.empty()){
                        readyQ.push_front(blockedQ.front());
                        blockedQ.pop_front();
                    }
                } else if (ss[0] == 'e' && ss[1] == 'n'){
                    tt -= t[4];
                    ff = false;
                } else {
                    tt -= t[0];
                    int loc = ss.find("=");
                    int value = atoi(ss.substr(loc + 1).c_str());
                    val[ss.front() - 'a'] = value;
                }
            }
            if (ff){
                readyQ.push_back(temp);
            }
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值