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 ' || ...
随机推荐
- poj 2104 K-th Number(主席树)
Description You are working for Macrohard company in data structures department. After failing your ...
- HW5.31
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- algorithm@ Divide two integers without using multiplication, division and mod operator. (Bit Operation)
#include<bits/stdc++.h> using namespace std; int divide(int dividend, int divisor) { long long ...
- Android Binder------ServiceManager启动分析
ServiceManager启动分析 简述: ServiceManager是一个全局的manager.调用了Jni函数,实现addServicew getService checkService ...
- INPUT输入框灰体提示
INPUT输入框灰体提示 <input type="text" value='15 words limit' style="color:#999999" ...
- Java流操作之转换流
流的操作规律: 1.明确流和目的. 数据源(源头):就是需要读取,可以使用两个体系:InputStream.Reader 数据汇(目的地):就是需要写入,可以使用两个体系:OutputStream.W ...
- 教程-经典Delphi教程网
有理想+志同道合的人+取长补短去协同工作=完美团队一流的项目 + 三流的执行者 = 垃圾项目三流的项目 + 一流的执行者 = 完美项目 自己公司网址:http://www.kaideruixin.ic ...
- Linux web工程部署远程必备软件安装
一.序 最近在将程序往linux上面部署,特此记录下部署步骤,待以后参考. web工程部署必备软件为:JDK.tomcat.数据库软件(oracle或mysql),远程监控.上传下载必备软件:VNC. ...
- MongoDB系列一(安装)
一.MongoDB在Windows平台下的安装: 安装包官方下载地址:http://www.mongodb.org/downloads 第一步:下载安装包:如果是win系统,注意是64位还是32位版本 ...
- 阿里巴巴笔试整理系列 Session2 高级篇
阿里一面:1. 入场就是红黑树,B数2. apache和nginx源码看过多少,平时看过什么技术论坛,还有没有看过更多的开源代码3. pthread 到自旋锁4. hadoop源码看过没5. 为什么选 ...