UVa 210 Concurrency Simulator (双端队列+模拟)
题意:给定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 (双端队列+模拟)的更多相关文章
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- bzoj 2457 [BeiJing2011]双端队列 模拟+贪心
[BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 457 Solved: 203[Submit][Status][D ...
- Uva - 210 - Concurrency Simulator
自己写个双端队列,或者直接用deque,这个也比较好用 AC代码: #include <iostream> #include <cstdio> #include <cst ...
- uva 210 - Concurrency Simulator (并行程序模拟)
from CSDN: https://blog.csdn.net/su_cicada/article/details/87898579 例题6-1 并行程序模拟( Concurrency Simula ...
- lintcode二叉树的锯齿形层次遍历 (双端队列)
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...
- 【BZOJ2457】[BeiJing2011]双端队列 贪心+模拟
[BZOJ2457][BeiJing2011]双端队列 Description Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若 ...
- Java 模拟队列(一般队列、双端队列、优先级队列)
队列: 先进先出,处理类似排队的问题,先排的.先处理,后排的等前面的处理完了,再处理 对于插入和移除操作的时间复杂度都为O(1).从后面插入,从前面移除 双端队列: 即在队列两端都能够insert和r ...
- 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 41844 Accepted: 12384 ...
- BZOJ2457 BeiJing2011 双端队列
[问题描述] Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. 她需要依次处理这N个数,对于每个数,Sherry能做以下两件事 ...
随机推荐
- vue基础——列表渲染
列表渲染 用 v-for 把一个数组对应为一组元素 我们用 v-for 指令根据一组数组的选项列表进行渲染.v-for 指令需要使用 item in items 形式的特殊语法, items 是源数据 ...
- linux 安装禅道
1. 查看Linux服务器版本信息 # cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 2. 禅道开源版安装包下载 # wge ...
- ntohs, ntohl, htons,htonl的比较和详解【转】
ntohs =net to host short int 16位 htons=host to net short int 16位 ntohs =net to host long int 32位 hto ...
- How to Pronounce the Word SOMETHING
How to Pronounce the Word SOMETHING Share Tweet Share Something tells me you’re going to like this v ...
- 一文看懂Stacking!(含Python代码)
一文看懂Stacking!(含Python代码) https://mp.weixin.qq.com/s/faQNTGgBZdZyyZscdhjwUQ
- webpack 常用插件及作用
copy-webpack-plugin :复制文件到目标文件夹.在开发时使用热模替换,(没有生成dist 文件夹,都在内存中),如果想引用某一个js文件,直接写script标签是找不到的,因为服务器内 ...
- css:多个div在同一行显示
使用float:left,也可以使用display : inline-block,可以使多个div在同一行显示. 示例如下: <div class="search_row"& ...
- hdoj1075-What Are You Talking About 【map】
http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...
- tf.random_normal()
tf.random_normal()函数用于从服从指定正太分布的数值中取出指定个数的值. tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf. ...
- Time range (447392) for take 'Take 001' is larger than maximum allowed(100000).
http://www.cnblogs.com/lopezycj/archive/2012/05/16/unity3d_tuchao.html https://forum.unity3d.com/thr ...