Uva - 210 - Concurrency Simulator
自己写个双端队列,或者直接用deque,这个也比较好用
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
using namespace std;
const int maxn = 1000;
int c[5];
int n, quantum, var[26], ip[maxn];
// ip[pid]是程序pid的当前行号
bool locked;
char prog[maxn][10];
deque<int> readyQ; // 因为阻止队列的程序是插入到等待队列的首部,所以不能用queue,要用deque
queue<int> blockQ;
void run(int pid)
{
int q = quantum;
// 每次最多运行quantum时间
while (q > 0) {
char *p = prog[ip[pid]];
switch (p[2]) {
case '=': // 赋值语句
// 赋值要考虑两位数和一位数的情况
var[p[0] - 'a'] = isdigit(p[5]) ? (p[4] - '0') * 10 + p[5] - '0' : p[4] - '0';
q -= c[0];
break;
case 'i': // 打印语句
printf("%d: %d\n", pid + 1, var[p[6] - 'a']);
q -= c[1];
break;
case 'c': // lock
if (locked) {
blockQ.push(pid);
return;
}
locked = true;
q -= c[2];
break;
case 'l': // unlock
locked = false;
if (!blockQ.empty()) {
int pid2 = blockQ.front();
blockQ.pop();
readyQ.push_front(pid2);
// deque的操作push_front,插入到队首
}
q -= c[3];
break;
case 'd':
return;
}
ip[pid]++;
}
readyQ.push_back(pid);
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d %d %d %d %d %d\n", &n, &c[0], &c[1], &c[2], &c[3], &c[4], &quantum);
memset(var, 0, sizeof(var));
int line = 0;
for (int i = 0; i < n; i++) {
fgets(prog[line++], maxn, stdin);
ip[i] = line - 1;
while (prog[line - 1][2] != 'd') { // 判断是否是end
fgets(prog[line++], maxn, stdin);
}
readyQ.push_back(i);
}
locked = false;
while (!readyQ.empty()) {
// 取出队首元素执行
int pid = readyQ.front();
readyQ.pop_front(); // 别忘了把队首出队
run(pid);
}
if (T) {
printf("\n");
}
}
return 0;
}
Uva - 210 - Concurrency Simulator的更多相关文章
- uva 210 - Concurrency Simulator (并行程序模拟)
from CSDN: https://blog.csdn.net/su_cicada/article/details/87898579 例题6-1 并行程序模拟( Concurrency Simula ...
- UVa 210 Concurrency Simulator (双端队列+模拟)
题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end. 变量用小写字母表示,初始化为0,为程序所公有( ...
- 210 - Concurrency Simulator(WF1991, deque, 模拟)
题目有点长,理解题花了不少时间 粘下别人的翻译~ 你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行.每个程序包含不超过25条语句,格式一共有5种: var=constant(赋值): pri ...
- UVA 11423 - Cache Simulator (树状数组)
UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...
- 【例题 6-1 UVA - 210】Concurrency Simulator
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 队列模拟题. 注意初始化.. 然后题目中是让读入一个数据组数然后再输入数据的. 但样例..但样例没有!? [代码] #include ...
- UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列
Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but ...
- 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991,Uva210)
任务介绍 你的任务是模拟n个程序的并行运算.(按照输入编号为1~n)的并行执行. 代码实现 #define LOCAL #include<bits/stdc++.h> using name ...
- UVa 210 并行程序模拟(deque)
题意: 模拟n个程序运行 格式一共有5种:var = constant(赋值):print var(打印):lock:unlock:end, 上述5种语句分别需要t1.t2.t3.t4.t5单位时间 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- IBM-x3650做RAID5更换硬盘后rebuild步骤分享
1.按Ctrl+H进入WebBIOS配置 2.点击start 3.点击Drives,对slot5进行配置 4.选择slot5,选择Properties,点击Go按钮 5.选择MakeUnconfGoo ...
- 数组中存放对象之java中定义类数组存放类
public class ClassArrayDemo { int age; String name; ClassArrayDemo(int age, String name) { this.age ...
- CentOS7.2安装mysql5.6
1.卸载系统自带的Mariadb [root@localhost~]# rpm -qa|grep mariadb //查询出已安装的mariadb [root@localhost~]# rpm -e ...
- Kafka(转载)
Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...
- 利用Express模拟web安全之---xss的攻与防
一.什么是XSS? 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意 ...
- A potentially dangerous Request.Form value was detected from the client问题处理
问题剖析: 用户在页面上提交表单到服务器时,服务器会检测到一些潜在的输入风险,例如使用富文本编辑器控件(RichTextBox.FreeTextBox.CuteEditor等)编辑的内容中包含有HTM ...
- SpringMVC之拦截器实现登录验证
今天回头看之前发的javaweb学习路线图,发现把路线图中的也学的有一半多了,不过还是路漫漫.在前面的博客中有学习过spring的aop,它利用动态代理实现,在springmvc中也是一样,今天使用H ...
- PHP 是什么
PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言. PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本 ...
- NavigationView使用过程的问题解决
NavigationView是android support design库提供的侧滑面板控件,通常与support v4库里的DrawerLayout侧滑控件搭配使用.以下是使用过程中遇到的问题及解 ...
- Bootstrap3 代码-代码块
多行代码可以使用 <pre> 标签.为了正确的展示代码,注意将尖括号做转义处理. <p>Sample text here...</p> <pre>< ...