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 ' || ...
随机推荐
- extern "C"的用法解析(转)
原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html 1.引言 C++语言的创建初衷是“a better C ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- soliworks三维机柜布局(三)绘制电气线路图
三维机柜布局中的自动布线是根据线路图中的电气连接属性布的.
- hdoj 1008 Elevator
Elevator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 转载MSDN 在ASP.NET 中执行 URL 重写
转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...
- java反射快速入门(二)
上一遍博文 , 简单介绍java 反射的常用接口,本遍博文, 我会结合项目开发的实际例子讲解下 java反射的使用 现在有个需求, 要将一个对象转换成xml格式, 或者将一串xml转换一个对象, 这时 ...
- 统计php源码行
嘿嘿,最近在提交文件,需要知道代码行数,简单记录下,由几种不同的方法进行: 1.直接在 linux 上运行下面语句即可,秒杀: find . -name "*.php" -exec ...
- 【转】移动前端手机输入法自带emoji表情字符处理
http://blog.csdn.net/binjly/article/details/47321043 今天,测试给我提了一个BUG,说移动端输入emoji表情无法提交.很早以前就有思考过,手机输入 ...
- 友盟分享 -QQAPI- QQApi.m:250 param error: url is nil
有一个项目 需要用到友盟分享,点击分享内容,需要跳转到指定的url,不带参数的url非常好跳,也没什么问题,但是 带了参数之后:比如http://121.43.121.8:8080/tj/photo/ ...
- 设置UITabBarController的背景颜色
if (IOS7) { self.tabBarController.tabBar.barTintColor = kTAB_BAR_GB_COLOR; }else{ self.tabBarControl ...