Concurrency Simulator(UVA 210)

网址如下:

Concurrency Simulator - UVA 210 - Virtual Judge (vjudge.net)

(第三方网站)

英语看得头疼

而且样例输入输出很坑,没有显示出case的数量的输入,而这个在input的描述里面是有的,测试的时候也是有的

最后我是不想整了,因为我是有用到getchar函数,需要考虑换行符的位置啥的,下面的代码只实现了一个case的处理

因为题目要求”把终止队列的第一个放在等待队列的第一个”,所以这题可以用到deque,也就是双端队列,可以进行队首队尾元素的出入而保证效率

但是能不用的话就不用,因为在排序上的效率较低,甚至不如复制到vector然后排序的效率高

但是这题只有元素的进出,问题不大

代码如下:

#include<iostream>
#include<string>
#include<deque>
#include<queue>
#include<map>
using namespace std;
deque<int> waitDeq;//等待队列
queue<int> stopQue;//阻止队列
map<char, int> variable;//变量
map<int, queue<string>> program;//程序
int n, t1, t2, t3, t4, t5, Q;
bool lockState = false;

int main(void)
{
    //输入
    scanf("%d%d%d%d%d%d%d", &n, &t1, &t2, &t3, &t4, &t5, &Q); getchar();
    for(int i = 1; i <= n; i++)
    {
        waitDeq.push_back(i); program[i] = queue<string>{};
        while(true)
        {
            string s; getline(cin, s);
            program[i].push(s);
            if(s == "end") break;
        }
    }
    //运行
    while(!waitDeq.empty())
    {
        int ID = waitDeq.front(), time = 0; waitDeq.pop_front();
        bool is_push = true;
        while(time < Q)
        {
            if(program[ID].front() == "lock")
            {
                if(lockState){stopQue.push(ID); is_push = false; break;}
                else{lockState = true; program[ID].pop(); time += t3;}
            }
            else if(program[ID].front() == "unlock")
            {
                lockState = false; time += t4; program[ID].pop();
                if(!stopQue.empty()) waitDeq.push_front(stopQue.front());
                stopQue.pop();
            }
            else if(program[ID].front() == "end")
            {
                is_push = false, time += t5;
                break;
            }
            else if((program[ID].front())[2] == '=')
            {
                int value = 0;
                for(int i = 4; i < program[ID].front().size(); i++)
                    value = value * 10 + (program[ID].front())[i] - '0';
                variable[(program[ID].front())[0]] = value; time += t1;
                program[ID].pop();
            }
            else
            {
                char c = (program[ID].front())[6];
                if(variable.count(c)) printf("%d: %d\n", ID, variable[c]);
                else printf("%d: 0", ID);
                time += t2; program[ID].pop();
            }
        }
        if(is_push) waitDeq.push_back(ID);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值