-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUVA-210.cpp
120 lines (113 loc) · 2 KB
/
UVA-210.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#include<string>
#include<string.h>
#include<deque>
#include<queue>
#include<vector>
#include<stdlib.h>
using namespace std;
int values[30];
deque<int> readys;
queue<int> blocks;
struct PROM {
vector<string> lines;
int cur;
};
vector<PROM> pros;
int stateTime[5] = {};
int quantum;
void init() {
memset(values, 0, 30*sizeof(int));
pros.clear();
readys.clear();
blocks = queue<int>();
memset(stateTime, 0, 5*sizeof(int));
quantum = 0;
}
void inputs() {
string line;
int t, i, j, k;
cin >> t;
for(i = 0; i < 5; ++i) {
cin >> stateTime[i];
}
cin >> quantum;
getchar();
for(i = 0; i < t; ++i) {
PROM p;
p.cur = 0;
do {
getline(cin, line);
p.lines.push_back(line);
} while(line != "end");
pros.push_back(p);
readys.push_back(i);
}
}
void assignment(string line) {
int a = line[0] - 'a';
values[a] = atoi(line.substr(4).c_str());
}
void print(int cnt, char c) {
cout << cnt+1 << ": " << values[c - 'a'] << endl;
}
void compute() {
int cnt, cntTime, i, j, k;
bool lockFlag = false, noReady;
string line;
while(!readys.empty()) {
cnt = readys.front();
readys.pop_front();
cntTime = quantum;
noReady = false;
while(cntTime > 0) {
line = pros[cnt].lines[pros[cnt].cur++];
if(line == "end") {
noReady = true;
break;
}
if(line[2] == '=') {
assignment(line);
cntTime -= stateTime[0];
}
if(line[1] == 'r') {
print(cnt, line[6]);
cntTime -= stateTime[1];
}
if(line == "lock") {
cntTime -= stateTime[2];
if(lockFlag == false) {
lockFlag = true;
} else {
--pros[cnt].cur;
blocks.push(cnt);
noReady = true;
break;
}
}
if(line == "unlock") {
cntTime -= stateTime[3];
lockFlag = false;
if(!blocks.empty()) {
k = blocks.front();
blocks.pop();
readys.push_front(k);
}
}
}
if(noReady == false) {
readys.push_back(cnt);
}
}
}
int main() {
int t, i, j;
cin >> t;
while(t--) {
init();
inputs();
compute();
if(t) cout << endl;
}
return 0;
}