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

粘下别人的翻译~

你的任务是模拟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. extern "C"的用法解析(转)

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

  2. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  3. soliworks三维机柜布局(三)绘制电气线路图

    三维机柜布局中的自动布线是根据线路图中的电气连接属性布的.

  4. hdoj 1008 Elevator

    Elevator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. 转载MSDN 在ASP.NET 中执行 URL 重写

    转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...

  6. java反射快速入门(二)

    上一遍博文 , 简单介绍java 反射的常用接口,本遍博文, 我会结合项目开发的实际例子讲解下 java反射的使用 现在有个需求, 要将一个对象转换成xml格式, 或者将一串xml转换一个对象, 这时 ...

  7. 统计php源码行

    嘿嘿,最近在提交文件,需要知道代码行数,简单记录下,由几种不同的方法进行: 1.直接在 linux 上运行下面语句即可,秒杀: find . -name "*.php" -exec ...

  8. 【转】移动前端手机输入法自带emoji表情字符处理

    http://blog.csdn.net/binjly/article/details/47321043 今天,测试给我提了一个BUG,说移动端输入emoji表情无法提交.很早以前就有思考过,手机输入 ...

  9. 友盟分享 -QQAPI- QQApi.m:250 param error: url is nil

    有一个项目 需要用到友盟分享,点击分享内容,需要跳转到指定的url,不带参数的url非常好跳,也没什么问题,但是 带了参数之后:比如http://121.43.121.8:8080/tj/photo/ ...

  10. 设置UITabBarController的背景颜色

    if (IOS7) { self.tabBarController.tabBar.barTintColor = kTAB_BAR_GB_COLOR; }else{ self.tabBarControl ...