UVA - 210 Concurrency Simulator(双端队列)

35 篇文章 0 订阅
3 篇文章 0 订阅

点击打开题目链接
用一个双端队列deque存等待队列,一个普通队列存阻止队列,然后进行各项操作的模拟
一开始用的结构体存每个程序,然后队列里面存的也是结构体,T了。然后改用vector存程序,队列存每个程序的标号。
代码:

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1000 + 5;
queue<int> stop;
deque<int> wait;
vector<string>ve[maxn];
string str;
int l[maxn],cnt[27], tim[5];
int T;
int inv;
int n, q;

void init() {
    memset(cnt, 0, sizeof(cnt));
    memset(l, 0, sizeof(l));
}

void input() {
    scanf("%d %d %d %d %d %d %d", &n, &tim[0], &tim[1], &tim[2], &tim[3], &tim[4], &q);
    for(int i = 0; i < n; i++) {
        ve[i].clear();
        while(getline(cin, str)) {
            if(str == "") continue;
             ve[i].push_back(str);
            if(str == "end")
                break;
        }
        wait.push_back(i);
    }
}

void solve(int i) {
    int cur = q, v;
    while(cur > 0) {
        str = ve[i][l[i]];
        //赋值
        if(str[2] == '=') {
            cur -= tim[0];
            int t = str[4] - '0';
            if(str.length() == 6) t = t * 10 + (str[5] - '0');
            cnt[str[0] - 'a'] = t;
        }
        //打印
        else if(str[2] == 'i') {
            cur -= tim[1];
            printf("%d: %d\n", i+1, cnt[str[6]-'a']);
        }
        //lock
        else if(str[2] == 'c') {
            cur -= tim[2];
            if(inv) {
                stop.push(i);
                return;
            }
            else 
                inv = 1;
        }
        //unlock
        else if(str[2] == 'l') {
            inv = 0;
            cur -= tim[3];
            if(!stop.empty()) {
                v = stop.front();
                stop.pop();
                wait.push_front(v);
            }
        }
        else return;
        l[i]++;
    }
    wait.push_back(i);
}

int main() {
    scanf("%d", &T);
    while(T--) {
        init();
        input();
        while(!wait.empty()) {
            int f = wait.front();
            wait.pop_front();
            solve(f);
        }
        if(T) cout << endl;
    }
    return 0; 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值