例题6-1 双端队列 uva210
2017-10-06 11:45
316 查看
Concurrency Simulator |
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.
Each statement requires an integral 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 expires 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 wishes 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. 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 unlockstatement. 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 andunlock statements.
(A renegade program with no lock/unlock pair could alter any variables 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 cases following, each of them as described below. This line is followed 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 programs which follow, the unit execution times 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 column of a line. Blanks appearing in a statement should be ignored. Associated with 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 separated 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 separate print statements should appear on separate lines.
A sample input and correct output are shown below.
Sample Input
1 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
思路:模拟
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <deque>
#include <vector>
using namespace std;
const int AX = 2e3+66;
queue <int> w;
deque <int> z; //执行队列
std::vector<string> v[AX];
int val[26];
bool lock ;
int limit;
int n , a , b , c , d , e, t; //= print lock unlcok end
void implement(){
int j[AX] = {0};
int id ;
memset(val,0,sizeof(val));
while( !z.empty() ){
int flag = 1;
limit = t ;
string temp;
id = z.front();
z.pop_front();
//cout << "id : " << id << endl;
while( limit > 0 ){
temp = v[id][j[id]];
if( temp[2] == '=' ){
limit -= a;
if( temp.size() == 6 ){
val[temp[0]-'a'] = (temp[4]-'0')*10 + ( temp[5] - '0' ) ;
}else{
val[temp[0]-'a'] = temp[4]-'0' ;
}
}else if( temp[0] == 'p' ){
limit -= b;
printf("%d: %d\n",id, val[temp[6]-'a']);
}else if( temp[0] == 'l' ){
limit -= c;
if( lock ){
w.push(id);
flag = 0;
break;
}else
lock = true;
}else if( temp[0] == 'u' ){
limit -= d;
lock = false;
if( !w.empty() ){
int u = w.front();
w.pop();
z.push_front(u) ;
}
}else{ flag = 0 ; break; }
j[id]++ ;
}
if(flag)
z.push_back(id);
}
}
int main(){
int T;
scanf("%d",&T);
while( T-- ){
string s;
scanf("%d%d%d%d%d%d%d",&n,&a,&b,&c,&d,&e,&t);
for( int i = 1 ; i <= n ; i++ ){
v[i].clear();
while(getline(cin,s)){
if( s == "" ) continue;
v[i].push_back(s);
if( s == "end" ) break;
}
z.push_back(i);
}
implement();
if( T ) puts("");
}
return 0;
}
相关文章推荐
- 【双端队列】例题6-1 UVa 210
- Uva210 Concurrency Simulator 【双端队列deque】【例题6-1】
- 紫书例题6-1 UVa210 ( 双端队列
- 例题6-1 并行程序模拟 UVa210 双端队列
- UVa210_Concurrency Simulator_双端队列的使用
- UVa 210 Concurrency Simulator(双端队列)
- UVA - 210 Concurrency Simulator(双端队列)
- UVa 210 Concurrency Simulator(双端队列)
- UVA - 210 - Concurrency Simulator <双端队列>
- UVa210 双端队列deque
- UVa 210 Concurrency Simulator(双端队列)
- 【日常学习】【双端队列】 Uva - 210 Concurrency Simulator题解
- UVa 210 - Concurrency Simulator(模拟+双端队列)
- UVA 11988 STL deque (双端队列||链表模拟)
- 指南 第三章 例题3 UVALive 3135 Argus(优先队列的应用)
- 紫书章五例题六 团队队列 uva 540(queue)
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- Uva136 Ugly Numbers 【优先队列】【例题5-7】
- 紫书章五例题六 团队队列 uva 540(queue)
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)