210 - Concurrency Simulator

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

chiahsun
New poster
Posts: 1
Joined: Tue Jan 05, 2016 10:16 pm

Re: 210 - Concurrency Simulator

Post by chiahsun »

For anyone still getting WAs.
Check this input:

Code: Select all

2

2 1 1 1 1 1 1
a = 1
a = 2
a = 3
a = 4
end
print a
print a
print a
print a
end

2 1 1 1 1 1 2
a = 1
a = 2
a = 3
a = 4
end
print a
print a
print a
print a
end
AC output

Code: Select all

2: 1
2: 2
2: 3
2: 4

2: 2
2: 2
2: 4
2: 4
Note that the inputs strictly follow the specification. No need to bother yourself.
Just pay attention to deal with multiple cases.
(1) A blank line to separate different output cases.
(2) Be sure that read the parameters successfully, or you use use the old parameters from previous case and still have code runnable.
(3) Push the front of the block queue to the front of ready queue.
(4) Not to remove the lock instruction if it is blocked. (Next time it should be execute again)

Another input:

Code: Select all

1

10 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
lock
z = 26
y = 25
x = 24
print x
print y
print z
unlock
end
k = 7
lock
k = 9
k = 10
print k
print k
unlock
end
k = 7
lock
k = 9
k = 10
print k
print k
unlock
end
end
k = 7
lock
k = 9
k = 10
print k
print k
unlock
end
k = 7
lock
k = 9
k = 10
print k
print k
unlock
end
k = 7
lock
k = 9
k = 10
print k
print k
unlock
end
AC output:

Code: Select all

1: 3
2: 3
3: 17
3: 5
4: 24
4: 25
4: 26
5: 10
5: 10
6: 10
6: 10
8: 10
8: 10
9: 10
9: 10
10: 10
10: 10
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21
ksqsf
New poster
Posts: 1
Joined: Thu Dec 08, 2016 11:40 am

Re: 210 - Concurrency Simulator

Post by ksqsf »

Hi! Could someone please provide more test data or tell me what is wrong... I checked all available data and my program below gave the right answers but I got WA on UVa OJ. Thanks in advance!

Code: Select all

#include <bits/stdc++.h>
using namespace std;

vector<string> program[100000];
int vars[26], pc[100000]; // PC register
int t1, t2, t3, t4, t5, Q, n;
bool locked = false;
deque<int> ready, blocked;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--) {
	scanf("%d%d%d%d%d%d%d", &n, &t1, &t2, &t3, &t4, &t5, &Q);
	for(int i = 0; i < n; ++i) {
	    for(;;) {
		string cmd;
		getline(cin, cmd);
		if(cmd.length() == 0) continue;
		program[i].push_back(cmd);
		if(cmd == "end") break;
	    }
	    ready.push_back(i);
	}
	memset(vars, 0, sizeof(vars));
	memset(pc, 0, sizeof(pc));
	while(n > 0) {
	    int P = 0;
	    int cur = ready.front(); ready.pop_front();
	    for(int j = pc[cur]; j < (int)program[cur].size(); ++j) {
		pc[cur] = j;
		if(P >= Q) {ready.push_back(cur); break;}
		string s1, s2;
		stringstream ss(program[cur][j]);
		ss >> s1;
		if(s1 == "end") {--n; break;}
		else if(s1 == "lock") {
		    if(locked) {blocked.push_back(cur); break;}
		    else locked = true;
		    P += t3;
		}
		else if(s1 == "unlock") {
		    if(blocked.size() > 0) {
			int first = blocked.front(); blocked.pop_front();
			ready.push_front(first);
		    }
		    locked = false;
		    P += t4;
		}
		else if(s1 == "print") {
		    ss >> s2;
		    printf("%d: %d\n", cur+1, vars[s2[0]-'a']);
		    P += t2;
		}
		else { // variable
		    int i3;
		    ss >> s2 >> i3;
		    vars[s1[0]-'a'] = i3;
		    P += t1;
		}
	    }
	}
	if(T) cout << endl;
    }
    return 0;
}
UPDATE: Luckily, I figured out what was wrong (Thanks to uDebug). The program vector was not cleared after one test case! Thus the former result affects the next program. Now AC. ;-)
Post Reply

Return to “Volume 2 (200-299)”