Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, executing some number of instructions from each program before switching to the next. You are to simulate the concurrent execution of up to ten programs on such a system and determine the output that they will produce.

The program that is currently being executed is said to be running, while all programs awaiting execution are said to be ready. A program consists of a sequence of no more than 25 statements, one per line, followed by an end statement. The statements available are listed below.

Statement Type    Syntax
Assignment      variable = constant
Output         print variable
Begin Mutual Exclusion lock
End Mutual Exclusion    unlock
Stop Execution             end
A variable is any single lowercase alphabetic character and a constant is an unsigned decimal number less than 100. There are only 26 variables in the computer system, and thay are shared among the programs. Thus assignments to a variable in one program affect the value that might be printed by a different program. All variables are initially set to zero.

Each statement requires an integeral number of time units to execute. The running program is permitted to continue executing instructions for a period of time called its quantum. When a program's time quantum expires, another ready program will be selected to run. Any instruction currently being executed when the time quantum expired will be allowed to complete.

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.
This order can change, however, as a result of the execution of lock and unlock statements.

The lock and unlock statements are used whenever a program wished to claim mutually exclusive access to the variables it is manipulating. These statements always occur in pairs, bracketing one or more other statements. A lock will always precede an unlock, and these statements will never be nested. One a program successfully execute 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. Programs blocked in this fashion lose any of their current time quantum remaining. When an unlock is executed, any program at the head of the blocked queue is moved to the head of the ready queue. The first statement this program will execute when it runs will be the lock statement that previously failed. Note that it is up to the programs involved to enforce the mutual exclusion protocol through correct usage of lock and unlock statements. (A renegade program with no lock/unlock pair could alter any variable it wished, despite the proper use of lock/unlock by the other programs)

Input

The input begins with a single positive integer on a line by itself indicating the number of the case following by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input file consists of seven integers separated by spaces. These integers specify (in order): the number of program which follow, the unit execution time for each of the five statements (in the order given above), and the number of time units comprising the time quantum. The remainder of the input consists of the programs, which are correctly formed from statements according to the rules described above.

All program statements begin in the first colum of a line.
Blanks appearing in a statement should be ignored.Associated which each program is an identification number based upon its location in the input data (the first program has ID = 1 the second has ID = 2, etc.).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be seperated by a blank line.

Your output will contain of the output generated by the print statements as they occur during the simulation. When a print statement is executed, your program should display the program ID, a colon, a space, and the value of the selected variable. Output from seperate print statement should appear on seperate lines.

A sample input and correct output are showed below.


Sample Input
3 1 1 1 1 1 1
a = 4 print a
lock
b = 9
print b
unlock
print b
end
a = 3
print a
lock
b = 8
print b
unlock
print b
end
b = 5
a = 17
print a
print b
lock
b = 21
print b
unlock
print b
end
Sample Output
1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21
题意:你的任务是模拟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<stdio.h>
#include<algorithm>
#include<iostream>
#include<string>
#include<string.h>
#include<deque>
#include<vector>
using namespace std;
deque<int>qr;
deque<int>qw;
vector<string>v[1005];
int var[26], p[1005], t[1005];
bool lock;
int Q,n,cas;
void run(int i)
{
int up = Q, w;
string cur;
while (up > 0)
{
cur = v[i][p[i]];//读取字符串
if (cur[2] == '=')
{
up -= t[0];
w = cur[4] - '0';
if (cur.size() == 6)
w = w * 10 + cur[5] - '0';
var[cur[0] - 'a'] = w;
}
else if (cur[2] == 'i')
{
up -= t[1];
printf("%d: %d\n", i, var[cur[6] - 'a']);
}
else if (cur[2] == 'c')
{
up -= t[2];
if (lock)
{
qw.push_back(i);
return;
}
lock = true;
}
else if (cur[2] == 'l')
{
lock = false;
up -= t[3];
if (!qw.empty())
{
w = qw.front();
qw.pop_front();
qr.push_front(w);
}
}
else return;
++p[i];
}
qr.push_back(i);//时间配额用完了就放到队尾
}
int main()
{
while (cin >> cas){
while (cas--)
{
cin >> n;
for (int i = 0; i < 5; i++)
cin >> t[i];
cin >> Q;
string s;
for (int i = 1; i <= n; i++)
{
v[i].clear();
while (getline(cin, s))
{
if (s == "")
continue;
v[i].push_back(s);
if (v[i].back() == "end")
break;
}
qr.push_back(i);
}
memset(p, 0, sizeof(p));
memset(var, 0, sizeof(var));
while (!qr.empty())
{
int cur = qr.front();
qr.pop_front();
run(cur);
}
if (cas)printf("\n");
}
}
return 0;
}

UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列的更多相关文章

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

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

  2. [算法竞赛入门经典]Message Decoding,ACM/ICPC World Finals 1991,UVa213

    Description Some message encoding schemes require that an encoded message be sent in two parts. The ...

  3. ACM - ICPC World Finals 2013 C Surely You Congest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 试题来源 ACM/ICPC World Fin ...

  4. ACM - ICPC World Finals 2013 A Self-Assembly

    原题下载 : http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这道题其实是2013年我AC的第一道题,非常的开心,这 ...

  5. ACM - ICPC World Finals 2013 F Low Power

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 有n个机器,每个机器有2个芯片,每个 ...

  6. ACM - ICPC World Finals 2013 I Pirate Chest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 海盗Dick受够了在公海上厮杀.抢劫 ...

  7. ACM - ICPC World Finals 2013 H Матрёшка

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...

  8. ACM - ICPC World Finals 2013 D Factors

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 一个最基本的算数法则就是大于1的整数 ...

  9. ACM - ICPC World Finals 2013 B Hey, Better Bettor

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这题真心的麻烦……程序不长但是推导过程比较复杂,不太好想 ...

随机推荐

  1. Ubuntu下快速部署安装 Nginx + PHP + MySQL 笔记

        先更新软件库 sudo apt-get update 安装 MySQL sudo apt-get install mysql-server 安装 Nginx sudo apt-get inst ...

  2. Java入门系列(一)基础概览

    序言 Java语言的特点不使用指针而使用引用.  

  3. IT人应当知道的10个行业小内幕

    如果你打算从事IT行业或刚进入这个行业,也许本文下面的小内幕会吓到你,因为这些事平常都不会公开讨论的.如果你是IT资深人士,或许你已经遇到其中的大部分了.如果你愿意,请一起来参与讨论吧. 这些内幕大多 ...

  4. 一些达成共识的JavaScript编码约定[转]

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  5. POJ 3783 Balls --扔鸡蛋问题 经典DP

    题目链接 这个问题是谷歌面试题的加强版,面试题问的是100层楼2个鸡蛋最坏扔多少次:传送门. 下面我们来研究下这个题,B个鸡蛋M层楼扔多少次. 题意:给定B (B <= 50) 个一样的球,从 ...

  6. MFC基于对话框风格按钮控件添加图片的方法(大神止步)

    菜鸟还在研究这个东西,大神就不要看了.一直都在觉得用VC或VS建立的对话框总是全灰色感觉太单调了,如果可以在上面添加一些漂亮的图片就好了,今天终于实现了.其实挺简单的,下面就分几个步骤讲一下: 第一步 ...

  7. C++ Primer 5th 第17章 标准库特殊设施

    C++新标准库提供了很多新功能,它们更加强大和易用. tuple类型 tuple是一种类似pair的模板,pair可以用来保存一对逻辑上有关联的元素对.但与pair不同的是,pair只能存储两个成员, ...

  8. imperva 非交互式导入导出配置

    非交互使用模式full_expimp.sh可以导出/导入手动使用交互式CLI 在root的命令行下执行: 例子:导出:# full_expimp.sh --operation=1 --pwd=密码 - ...

  9. Linux USB驱动学习总结(一)---- USB基本概念及驱动架构

    USB,Universal Serial Bus(通用串行总线),是一个外部总线标准,用于规范电脑与外部设备的连接和通讯.是应用在PC领域的接口技术.USB接口支持设备的即插即用和热插拔功能.USB是 ...

  10. MVVM模式的命令绑定

    命令绑定要达到的效果 命令绑定要关注的核心就是两个方面的问题,命令能否执行和命令怎么执行.也就是说当View中的一个Button绑定了ViewModel中一个命令后,什么时候这个Button是可用的, ...