自己写个双端队列,或者直接用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的更多相关文章

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

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

  2. UVa 210 Concurrency Simulator (双端队列+模拟)

    题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end. 变量用小写字母表示,初始化为0,为程序所公有( ...

  3. 210 - Concurrency Simulator(WF1991, deque, 模拟)

    题目有点长,理解题花了不少时间 粘下别人的翻译~ 你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行.每个程序包含不超过25条语句,格式一共有5种: var=constant(赋值): pri ...

  4. UVA 11423 - Cache Simulator (树状数组)

    UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...

  5. 【例题 6-1 UVA - 210】Concurrency Simulator

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 队列模拟题. 注意初始化.. 然后题目中是让读入一个数据组数然后再输入数据的. 但样例..但样例没有!? [代码] #include ...

  6. UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列

    Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but ...

  7. 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991,Uva210)

    任务介绍 你的任务是模拟n个程序的并行运算.(按照输入编号为1~n)的并行执行. 代码实现 #define LOCAL #include<bits/stdc++.h> using name ...

  8. UVa 210 并行程序模拟(deque)

    题意: 模拟n个程序运行 格式一共有5种:var = constant(赋值):print var(打印):lock:unlock:end, 上述5种语句分别需要t1.t2.t3.t4.t5单位时间 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. JSON概述

    错误理解:         一直以为JSON就是对象,拥有跟js对象类似的特征:{key:value}形式, 以至于在自己的思维定式中就出现了一种很可怕的情景:居然不知道怎么去解释习以为常的json是 ...

  2. css修改浏览器默认的滚动条样式

    //滚动条样式 ::-webkit-scrollbar { width: 10px; } /* 垂直滚动条的滑动块 */ ::-webkit-scrollbar-thumb:vertical { bo ...

  3. MYSQL 表左连接 ON AND 和ON WHERE 的区别

    首先是针对左右连接,这里与inner join区分 在使用left join时,on and 和on where会有区别 1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条 ...

  4. Lucene初体验——Hello Word实现

    1.创建索引 /** * 建立索引 */ public void index(){ IndexWriter writer=null; try { //1.创建Directory //Directory ...

  5. 搭建一个交互式的前端构建环境.md

    为了提高开发效率.减少重复的操作,现在几乎全部的前端项目都需要依赖一些构建工具来实现自动化打包,主流的有webpack, gulp, grunt等.加上各种各样的配置文件就会形成了一个相对复杂的构建环 ...

  6. vue-cli搭建项目的目录结构及说明

    vue-cli基于webpack搭建项目的目录结构 build文件夹 ├── build              // 项目构建的(webpack)相关代码    │ ├── build.js   ...

  7. MySQL/MariaDB 在插入数据的时候提示 Incorrect string value

    现象 今天开新工程,建表的时候弹出这玩意: what's this? 看起来好像是说我传入的内容不对? 可是仔细看看内容,没乱码,标准的中文字符串.想来是编码问题. 经过修改编码后,解决了问题. 解决 ...

  8. Gleb And Pizza CodeForces - 842B

    CodeForces - 842B #include<bits/stdc++.h> using namespace std; int main() { int r,d,t; double ...

  9. postgresql 登录查看表定义

    su - postgres psql \connect database_name; \d table_name

  10. Bootstrap3 排版-地址

    让联系信息以最接近日常使用的格式呈现.在每行结尾添加 可以保留需要的样式. Twitter, Inc. 795 Folsom Ave, Suite 600 San Francisco, CA 9410 ...