Test instructions: Given n programs, each program has five operations, var = constant (Assignment), print var (print), lock, Unlock,end.
Variables are represented in lowercase letters, initialized to 0, and are public to the program (a variable in a program can affect this variable in other programs).
The constant is less than 100 (that is, a maximum of two digits).
Each moment can only have one program in the running state, others are waiting, the above five operations are T1, T2, T3, T4, T5. Running programs,
Can run at most Q time, when the Q time is exhausted, it will be placed at the end of the waiting queue, and then take out a program from the first run, the initial waiting queue in the order of entry,
But lock and unlock change the order, they always appear in pairs and do not appear nested. If a program has already executed lock, then there is a program to perform lock,
Then the program is immediately placed at the end of a block queue (which, of course, is wasted if the run time is not over). When unlock is finished, the first program in the queue is blocked from entering the header of the wait queue.
Ask you what the program is running results, the output format is the number of programs with a colon plus a space plus the result, two connected data separated by a blank line.
Analysis: The main problem is to understand the test instructions, if you understand test instructions, then it is relatively simple, the first is the topic in the queue, and also put in the header, then you can know it should be a double-ended queue,
We use STL inside, can also write one ourselves, not difficult to write, the other is to simulate this, there is no difficulty, to note that at the end of the Inlock from the blocking queue to put in the waiting queue,
Consider whether it is empty or it may cause the program to crash.
The code is as follows:
#include <iostream> #include <cstdio> #include <queue> #include <deque> #include <vector> #include <cstring>using namespace Std;const int maxn = 1024;deque<int> wait;queue<int> block;vector <string> Pram[maxn];int t[6], Q, CNT[MAXN], Val[30];bool lock;void run (int i) {int Q = q; string S; while (q > 0) {s = pram[i][cnt[i]]; if (' = ' = = = S[2]) {int num = s[4]-' 0 '; Q-= t[1]; if (6 = = s.size ()) num = ten * num + s[5]-' 0 '; Val[s[0]-' a '] = num; } else if (' i ' = = s[2]) {Q-= t[2]; printf ("%d:%d\n", I, val[s[6]-' a ']); } else if (' c ' = = S[2]) {Q-= t[3]; if (lock) {Block.push (i); return; } else lock = true; } else if (' l ' = = s[2]) {Q-= t[4]; lock = false; if (!block.empty ()) {Wait.push_front (block. Front ()); Block.pop (); }} else return; ++cnt[i]; } wait.push_back (i);} int main () {int T, n; Cin >> T; while (t--) {scanf ("%d", &n); for (int i = 0; i < 5; ++i) scanf ("%d", &t[i+1]); scanf ("%d", &q); GetChar (); string S; for (int i = 1; I <= n; ++i) pram[i].clear (); for (int i = 1; I <= n; ++i) {while (true) {getline (CIN, s); Pram[i].push_back (s); if (s = = "End") break; } wait.push_back (i); } memset (CNT, 0, sizeof (CNT)); Memset (val, 0, sizeof (Val)); lock = false; while (!wait.empty ()) {int p = Wait.front (); Wait.pop_front (); Run (p); } if (T) printf ("\ n"); } return 0;}
UVa Concurrency Simulator (double-ended queue + analog)