题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end。

变量用小写字母表示,初始化为0,为程序所公有(一个程序里对某个变量修改可以会影响其他程序里的这个变量),

常数小于100(也就是说最多两位数)。

每个时刻都只能有一个程序处于运行状态,其他的都在等待,上述五种操作用时分别是t1, t2, t3, t4, t5。运行中的程序,

每次最多能运行q个时间,当q个时间被用完后,它会被放在等待队列的尾部,然后再从首部取出一个程序运行,初始等待队列按输入顺序,

但是lock和unlock会改变顺序,它们总是成对出现,不会出现嵌套。如果某个程序已经执行了lock,后面还有程序执行lock,

那么这个程序就会马上被放到一个阻止队列的尾部(当然如果运行时间还没用完也就浪费了)。当unlock结束后,阻止队列中的第一个程序进入等待队列的首部。

问你程序的运行结果是什么,输出格式是第几个程序加冒号加空格加结果,两个相连的数据用空行隔开。

析:这个题主要是看懂题意,如果看懂题意,那么就比较简单了,首先是题目中多次出现队列,并且还放在首部,那么就可以知道应该是双端队列,

我们使用STL里面的,也可以自己写一个,并不难写,其他的就是模拟这个,没有什么难度,要注意的是,在结束inlock从阻止队列中拿出放在等待队列时,

要考虑是不是已经空了,否则可能使程序崩溃。

代码如下:

#include <iostream>
#include <cstdio>
#include <queue>
#include <deque>
#include <vector>
#include <cstring> using namespace std;
const int maxn = 1024;
deque<int> wait;
queue<int> block;
vector<string> pram[maxn];
int t[6], Q, cnt[maxn], val[30];
bool lock; void run(int i){
int q = Q;
string s;
while(q > 0){
s = pram[i][cnt[i]];
if('=' == s[2]){
int num = s[4] - '0';
q -= t[1];
if(6 == s.size()) num = 10 * num + s[5] - '0';
val[s[0]-'a'] = num;
}
else if('i' == s[2]){
q -= t[2];
printf("%d: %d\n", i, val[s[6]-'a']);
}
else if('c' == s[2]){
q -= t[3];
if(lock){
block.push(i);
return ;
}
else lock = true;
}
else if('l' == s[2]){
q -= t[4];
lock = false;
if(!block.empty()){
wait.push_front(block.front());
block.pop();
}
}
else return ;
++cnt[i];
}
wait.push_back(i);
} int main(){
int T, n; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < 5; ++i) scanf("%d", &t[i+1]);
scanf("%d", &Q);
getchar();
string s;
for(int i = 1; i <= n; ++i) pram[i].clear();
for(int i = 1; i <= n; ++i){
while(true){
getline(cin, s);
pram[i].push_back(s);
if(s == "end") break;
}
wait.push_back(i);
} memset(cnt, 0, sizeof(cnt));
memset(val, 0, sizeof(val));
lock = false;
while(!wait.empty()){
int p = wait.front(); wait.pop_front();
run(p);
}
if(T) printf("\n");
}
return 0;
}

UVa 210 Concurrency Simulator (双端队列+模拟)的更多相关文章

  1. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. bzoj 2457 [BeiJing2011]双端队列 模拟+贪心

    [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 457  Solved: 203[Submit][Status][D ...

  3. Uva - 210 - Concurrency Simulator

    自己写个双端队列,或者直接用deque,这个也比较好用 AC代码: #include <iostream> #include <cstdio> #include <cst ...

  4. uva 210 - Concurrency Simulator (并行程序模拟)

    from CSDN: https://blog.csdn.net/su_cicada/article/details/87898579 例题6-1 并行程序模拟( Concurrency Simula ...

  5. lintcode二叉树的锯齿形层次遍历 (双端队列)

    题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...

  6. 【BZOJ2457】[BeiJing2011]双端队列 贪心+模拟

    [BZOJ2457][BeiJing2011]双端队列 Description        Sherry现在碰到了一个棘手的问题,有N个整数需要排序.        Sherry手头能用的工具就是若 ...

  7. Java 模拟队列(一般队列、双端队列、优先级队列)

    队列: 先进先出,处理类似排队的问题,先排的.先处理,后排的等前面的处理完了,再处理 对于插入和移除操作的时间复杂度都为O(1).从后面插入,从前面移除 双端队列: 即在队列两端都能够insert和r ...

  8. 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41844   Accepted: 12384 ...

  9. BZOJ2457 BeiJing2011 双端队列

    [问题描述] Sherry现在碰到了一个棘手的问题,有N个整数需要排序.  Sherry手头能用的工具就是若干个双端队列.        她需要依次处理这N个数,对于每个数,Sherry能做以下两件事 ...

随机推荐

  1. event bManualResult

    MSDN: bManualReset [in] If this parameter is TRUE, the function creates a manual-reset event object, ...

  2. SQL语言类别

    SQL语言主要分为四大类:数据查询语言DQL,数据操纵语言DML, 数据定义语言DDL,数据控制语言DCL. DQL (data query language) DML(data manipulati ...

  3. How to Pronounce WH Words — what, why, which

    How to Pronounce WH Words — what, why, which Share Tweet Share Have you noticed that there are two d ...

  4. spring 中的断言的作用

    org.springframework.util.AssertAssert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了.就是断定某一个实际的值就为自己预期想得到的,如 ...

  5. Spring Boot 连接池

    配置方法 基于当前的1.5.2.RELEASE的Spring Boot. 依照官方文档,如果增加了如下依赖的配置,或者类路径中存在spring-boot-starter-jdbc的jar,那么已默认启 ...

  6. oracle,PL/SQL新建表

    创建用户 -- Create the user create user U_HQ_JAVA default tablespace USERS temporary tablespace TEMP pro ...

  7. HashCode的理解

    一.hashcode是什么 要理解hashcode首先要理解hash表这个概念 1. 哈希表 hash表也称散列表(Hash table),是根据关键码值(Key value)而直接进行访问的数据结构 ...

  8. Sorting(好题)

    Sorting https://www.zhixincode.com/contest/21/problem/I?problem_id=324 题目描述 你有一个数列a_1, a_2, \dots, a ...

  9. discuz的css处理机制

    common/common.css 是一个通用的css文件. common/module.css 是某个功能模块使用的css文件.   module.css中,利用特殊语法: /** 标识 **/ c ...

  10. 在ecplise中创建一个maven工程

    1.我们首先需要在Ecplise中配置maven环境,详情见我的博客:https://www.cnblogs.com/wyhluckdog/p/10277278.html 2.maven projec ...