题意:本题需要你模拟一些简单程序,每一个程序有以下5种指令:
var = val,给变量赋值,简单起见保证变量名为一个字母,变量为所有进程共用,并且初始为0,保证val是不大于100的正整数;
print var,输出变量var;
lock对所有变量申请独占访问(不影响赋值和打印)
unlock解除独占访问
end结束程序
以上指令分别耗时t1,t2,t3,t4,t5的时间,一开始进程按照输入顺序依次插入到等待队列中,每次从等待队列队首选择一个进程执行。每个进程有一个配额(限定时间)Q,当配额用完时,该进程会在执行完当前语句后立即被插入到一个等待队列尾部中。
但是lock语句和unlock语句会改变进程的执行顺序。当一个程序执行了lock语句,其他进程再执行到lock语句时会被立即插入到一个阻止队列队尾,当程序执行到unlock语句时,阻止队列的队首的第一个进程会被立即插入到等待队列队首。
第一行为测试数据组数
第二行有7个正整数分别是程序数N(1<=N<=1000),t1,t2,t3,t4,t5,配额Q;
接下来是N个程序,每个程序以end结尾,每个程序保证不超过25条指令
输出所有print语句的结果格式id: val,id为进程编号(按照输入顺序且从1开始)每组测试数据之间应该有一行空行
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
1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
var = val,给变量赋值,简单起见保证变量名为一个字母,变量为所有进程共用,并且初始为0,保证val是不大于100的正整数;
print var,输出变量var;
lock对所有变量申请独占访问(不影响赋值和打印)
unlock解除独占访问
end结束程序
以上指令分别耗时t1,t2,t3,t4,t5的时间,一开始进程按照输入顺序依次插入到等待队列中,每次从等待队列队首选择一个进程执行。每个进程有一个配额(限定时间)Q,当配额用完时,该进程会在执行完当前语句后立即被插入到一个等待队列尾部中。
但是lock语句和unlock语句会改变进程的执行顺序。当一个程序执行了lock语句,其他进程再执行到lock语句时会被立即插入到一个阻止队列队尾,当程序执行到unlock语句时,阻止队列的队首的第一个进程会被立即插入到等待队列队首。
第一行为测试数据组数
第二行有7个正整数分别是程序数N(1<=N<=1000),t1,t2,t3,t4,t5,配额Q;
接下来是N个程序,每个程序以end结尾,每个程序保证不超过25条指令
输出所有print语句的结果格式id: val,id为进程编号(按照输入顺序且从1开始)每组测试数据之间应该有一行空行
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
1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21
思路:双端队列的应用
#include <iostream> #include <stdio.h> #include <vector> #include <queue> #include <deque> #include <string.h> using namespace std; //记录n个程序的语句 char pro[1005][25]; //变量值和第i个程序将要执行的语句编号 int var[26],ru[1005]; //等待队列 deque<int> dq; //阻塞队列 queue<int> q; int n,t1,t2,t3,t4,t5,Q; //是否lock bool flag; void run(int pid){ int time = Q; while(time > 0){ char *p = pro[ru[pid]]; if(p[2] == '='){ int temp = p[4] - '0'; if(isdigit(p[5])){ temp = temp * 10 + p[5] - '0'; } var[p[0] - 'a'] = temp; time -= t1; } else if(p[2] == 'i'){ printf("%d: %d\n",pid + 1,var[p[6] - 'a']); time -= t2; } else if(p[2] == 'c'){ if(flag){ q.push(pid); return; } flag = 1; time -= t3; } else if(p[2] == 'l'){ flag = false; if(!q.empty()){ int pid2 = q.front(); q.pop(); dq.push_front(pid2); } time -= t4; } else if(p[2] == 'd'){ return; } ru[pid]++; } dq.push_back(pid); } int main(void){ int t; scanf("%d",&t); while(t--){ scanf("%d%d%d%d%d%d%d",&n,&t1,&t2,&t3,&t4,&t5,&Q); memset(var,0,sizeof(var)); int cnt = 0; for(int i = 0; i < n; i++){ fgets(pro[cnt++],1000,stdin); ru[i] = cnt - 1; while(pro[cnt - 1][2] != 'd'){ fgets(pro[cnt++],1000,stdin); } dq.push_back(i); } flag = 0; while(!dq.empty()){ int pid = dq.front(); dq.pop_front(); run(pid); } if(t){ printf("\n"); } } return 0; }