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 ...
随机推荐
- Maven的pom.xml文件结构之基本配置packaging和多模块聚合结构(微服务)
1. packaging packaging给出了项目的打包类型,即作为项目的发布形式,其可能的类型.在Maven 3中,其可用的打包类型如下: jar,默认类型 war ejb ear rar pa ...
- SSH构造struts2项目
第一在pom.xml导入相应的包 (网上有很多导入多个包的教程,我缩减到一个了) <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- event工具集
eventTool = { // 页面加载完成后 readyEvent : function(fn) { if (fn==null) { fn=document; } var oldonload = ...
- 重写轮子之 kNN
# !/usr/bin/python # -*- coding:utf-8 -*- """ Re-implement kNN algorithm as a practic ...
- Why Helm? - 每天5分钟玩转 Docker 容器技术(160)
本章我们将学习 Helm,Kubernetes 的包管理器. 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Hel ...
- map函数、filer函数、reduce函数的用法和区别
Map函数 map函数的用法如下: def add_one(x): return x+1 #使用普通函数 v1 = map(add_one,[1,2,3]) v1 = list(v1) print(v ...
- 匿名函数lambda
匿名函数的定义 在python中,匿名函数的定义如下: func =lambda x:x+1 #定义匿名函数,x为传参,x+1为返回值,func为函数名 res = func(10) #执行匿名函数 ...
- Docker rancher 部署
Docker-rancher #环境 centos7.4 , Docker version 17.12.0-ce #下载docker镜像 docker pull mysql:5.7 docker pu ...
- Android Multimedia框架总结(二十二)MediaCodec中C++中创建到start过程及状态变换
上一章介绍MediaCodec中创建到start过程(到jni部分),从今天开始,将深入源码中看看其c++过程,看下Agenda如下: mediacodec.h CreateByType initMe ...
- 给定一数组,输出满足2a=b(a,b代表数组中的数)的数对,要求时间复杂度尽量低。
//时间复杂度O(n),空间复杂度O(n) void findSequence(int* arr, int len) { int* hashtable = new int[RANGE]; memset ...