题目有点长,理解题花了不少时间

粘下别人的翻译~

你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行。每个程序包含不超过25条语句,格式一共有5种:

  var=constant(赋值);

  print  var(打印);

  lock;

  unlock;

  end。

变量用单个小写字母表示,初始值为0,为所有程序公有(因此在一个程序里对某个变量赋值可能会影响到另一个程序)。常数是小于100的非负整数。
每个时刻只能有一个程序处于运行态,其他程序均处于等待。上述五种语句分别需要t1、t2、t3、t4、t5单位时间。运行态的程序每次最多运行Q个单位时间(成为配额)。当一个程序的配额用完之后,把当前语句(如果存在)执行完之后该程序会被插入一个等待队列中,然后处理器从队首取出一个程序继续执行。初 始等待队列包含按输入顺序排列的各个程序,但由于lock和unlock语句的出现,这个序列可能会改变。
lock的作用是申请对所有变量的独占访问。lock和unlock总是成对出现,并且不会嵌套。lock总是在unlock的前面。当一个程序成功执行完lock指令后,其他程序一旦试图执行lock指令,就会马上被放到一个所谓的阻止队列的尾部(没有用完的配额就浪费了),当unlock指令执行完毕后,阻止队列的第一个程序进入等待队列的首部。
输入n、t1、t2、t3、t4、t5Q以及n个程序,按照时间顺序输出所有print语句的程序编号和结果。
 
附代码如下:
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cstring>
#include<deque>
using namespace std;
const int maxn = ;
int n, t1, t2, t3, t4, t5, Q;
char pro[maxn][];
int ID[maxn];
int var[];
bool locked;
deque<int> ReadyQ;
deque<int> BlockQ; void Run(int id)
{
int q = Q;
while(q > )
{
char *ins = pro[ID[id]];
switch(ins[])
{
case '='://var = constant;
{
/*通过数组var进行各变量值的记录*/
int buf = ;
for(int i = ; i < strlen(ins)-; i++)
buf = buf*+(ins[i]-'');
var[ins[]-'a'] = buf;
q -= t1;
break;
}
case 'i'://print var;
{
printf("%d: %d\n", id+, var[ins[]-'a']);
q -= t2;
break;
}
case 'c'://lock
{
/*Once a program successfully executes a lock statement, no other program may successfully execute a lock statement
until the locking program runs and executes the corresponding unlock statement.
Should a running program attempt to execute a lock while one is already in effect,
this program will be placed at the end of the blocked queue.*/
if(locked)
{
BlockQ.push_back(id);
return;
}
locked = true;
q -= t3;
break;
}
case 'l'://unlock;
{
locked = false;
/*When an unlock is executed, any program at the head of the blocked queue is moved to the head of the ready queue. */
if(!BlockQ.empty())
{
ReadyQ.push_front(BlockQ.front());
BlockQ.pop_front();
}
q -= t4;
break;
}
case 'd'://end;
{
q -= t5;
return;
break;
}
}//switch;
ID[id]++;
}//while;
/*When a program time quantum expires, another ready program will be selected to run.
Any instruction currently being executed when the time quantum expires will be allowed to complete. */
ReadyQ.push_back(id);
}//Run; int main()
{
int T;
scanf("%d", &T);
for(int cases = ; cases < T; cases++)
{
memset(var, , sizeof(var));
if(cases) printf("\n");
scanf("%d%d%d%d%d%d%d", &n, &t1, &t2, &t3, &t4, &t5, &Q);
int line = ;
for(int i = ; i < n; i++)
{
///注意记录多行字符串方法
///================================================
fgets(pro[line++], maxn, stdin);
ID[i] = line-; ///line可记录某ID开始到最后的操作;
/*identification number based upon its location in the input data.
(the first program has ID = 1, the second has ID = 2, etc.)*/
while(pro[line-][] != 'd')
fgets(pro[line++], maxn, stdin);
/*Programs are queued first-in-first-out for execution in a ready queue.
The initial order of the ready queue corresponds to the original order of the programs in the input file.*/
///================================================
ReadyQ.push_back(i);
}
locked = false;
while(!ReadyQ.empty())
{
int Pro_id = ReadyQ.front();
ReadyQ.pop_front();
Run(Pro_id);
}
}
return ;
}

210 - Concurrency Simulator(WF1991, deque, 模拟)的更多相关文章

  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. Uva - 210 - Concurrency Simulator

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

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

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

  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. LinkedList(实现了queue,deque接口,List接口)实现栈和队列的功能

    LinkedList是用双向链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢. 底层是一个双向链表,链表擅长插入和删除操作,队列和栈最常用的2种操作都设计到插入和删除 impo ...

  8. ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)

    问题 A: (ds:队列)打印队列 时间限制: 1 Sec  内存限制: 128 MB 提交: 25  解决: 4 [提交][状态][讨论版] 题目描述 网络工程实验室只有一台打印机,它承担了非常繁重 ...

  9. 生产上数据库大量的latch free 导致的CPU资源耗尽的问题的解决

    中午的时候,我们生产上的某个数据库,cpu一直居高不下 通过例如以下的sql语句,我们查看当时数据库的等待,争用的情况: select s.SID, s.SERIAL#, 'kill -9 ' || ...

随机推荐

  1. 用户名 不在 sudoers文件中,此事将被报告。

    原文解决方法:http://blog.csdn.net/lincyang/article/details/21020295 CentOS7.0 用到sudo权限的时候出现的问题,如题. sudo命令可 ...

  2. extern "C"的用法解析(转)

    原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html   1.引言 C++语言的创建初衷是“a better C ...

  3. curl命令学习(转载的)

    原文地址: http://www.thegeekstuff.com/2012/04/curl-examples/ curl是网络上常用一个命令,简单来说就是可以上传下载,甚至可以当成下载工具使用,比如 ...

  4. 2014年国人开发的最热门的.NET开源项目 TOP 25

    原文地址:http://www.cnphp6.com/archives/72213 1 奎宇工作室 / DotNetCodes C# 一些常用的功能性代码,可以减少许多开发时间,而且类与类之间没有什么 ...

  5. bzoj 1833 [ZJOI2010]count 数字计数(数位DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1833 [题意] 统计[a,b]区间内各数位出现的次数. [思路] 设f[i][j][k ...

  6. NOIP2006 金明的预算方案

    1.             金明的预算方案 (budget.pas/c/cpp) [问题描述] 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈 ...

  7. 【VMware虚拟机】【克隆问题】在VMware 9.0下克隆CentOS6.5虚拟机无法识别eth网卡

    [日期]2014年4月23日 [平台]windows 8 vmware workstation 9.0 centos 6.5 [日志]1)执行ifconfig命令,输出: 2)查看/etc/udev/ ...

  8. android 文件上传

    1.java原生上传 拼接上传的字符串 2.HttpClient方式上传 1.导入httpClient jar(core.mime)包 2.设置FileBody.MultiPartEntity.发送请 ...

  9. Android自定义下拉刷新

    网上的下拉刷新功能很多,不过基本上都是隐藏header的,而项目里面需要只隐藏部分的header,类似QQ好友动态的效果,修改了一些现有的,最后有很多问题,所以就自己自定义了一个,逻辑也很简单,首先就 ...

  10. [威客任务]¥800.00 JS实现网站联动三级选项

    任务地址:http://task.zhubajie.com/3275832/ 具体要求: 要求1) 选单样式参照附件2) 点击第一个选项内容后,跳出第二个选项栏位,并自动更新选项内容3) 点击第二个选 ...