210 - Concurrency Simulator(WF1991, deque, 模拟)
题目有点长,理解题花了不少时间
粘下别人的翻译~
你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行。每个程序包含不超过25条语句,格式一共有5种:
var=constant(赋值);
print var(打印);
lock;
unlock;
end。
#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, 模拟)的更多相关文章
- 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,为程序所公有( ...
- Uva - 210 - Concurrency Simulator
自己写个双端队列,或者直接用deque,这个也比较好用 AC代码: #include <iostream> #include <cstdio> #include <cst ...
- 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991,Uva210)
任务介绍 你的任务是模拟n个程序的并行运算.(按照输入编号为1~n)的并行执行. 代码实现 #define LOCAL #include<bits/stdc++.h> using name ...
- 【例题 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 ...
- LinkedList(实现了queue,deque接口,List接口)实现栈和队列的功能
LinkedList是用双向链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢. 底层是一个双向链表,链表擅长插入和删除操作,队列和栈最常用的2种操作都设计到插入和删除 impo ...
- ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)
问题 A: (ds:队列)打印队列 时间限制: 1 Sec 内存限制: 128 MB 提交: 25 解决: 4 [提交][状态][讨论版] 题目描述 网络工程实验室只有一台打印机,它承担了非常繁重 ...
- 生产上数据库大量的latch free 导致的CPU资源耗尽的问题的解决
中午的时候,我们生产上的某个数据库,cpu一直居高不下 通过例如以下的sql语句,我们查看当时数据库的等待,争用的情况: select s.SID, s.SERIAL#, 'kill -9 ' || ...
随机推荐
- iOS生命周期
1.application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次.若用户直接启动,lauchOptions内无 ...
- 关于C#动态调用VC Dll的方法(转)
http://blog.csdn.net/null1/article/details/3953155
- [iOS基础控件 - 5.2] 查看大图、缩放图片代码(UIScrollView制作)
原图: 900 x 1305 拖曳滚动: 缩放: 主要代码: // // ViewController.m // ImageZoom // // Created by ...
- JLink 在J-Flash ARM批处理自动下载
"C:\Program Files\SEGGER\JLinkARM_V420c\jflasharm.exe" -openprj.\stm32f100c8.jflash -open. ...
- 类型检测汇总!typeof 和 instanceof 和isArray
, ]; alert(arr instanceof Array);//true 以上老方法判断是否是数组,存在一个问题,就是它只适用于单执行环境(窗口),如果该窗口有其他框架(比如 iframe)则会 ...
- matlab inpolygon 判断点在多边形内
如何判断一个点在多边形内部? xv= [0 3 3 0 0]; %x坐标 yv= [0 0 3 3 0];%y坐标 x=1.5; y=1.5; in=inpolygon(x,y,xv,yv) plot ...
- MySQL通过RPM安装
以前写过一篇文章,RedHat Linux 6.1 安装MySQL,本文是从解决依赖的角度上再次描述如何在Linux下以RPM包方式安装MySQL. [root@serv01 ~]# ls /iso/ ...
- cocos2d-x 纹理深入研究
转自:http://blog.csdn.net/qq51931373/article/details/9152227 1.纹理控制. 看此代码: CCSprite *pSprite = CCSprit ...
- iOS8 CLLocationManager 、CLGeocoder获取地理位置
最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下 ...
- linux centos6 NAT 端口转发
有很多时候我们为了安全,需要将例如数据库服务器放到内网中.但是有些时候又系统给外网开一个端口,这时就可以利用外网的服务器进行一个端口转发.今天我们以centos6 为例进行端口转发配置. 首先vi / ...