网址如下:
Concurrency Simulator - UVA 210 - Virtual Judge (vjudge.net)
(第三方网站)
英语看得头疼
而且样例输入输出很坑,没有显示出case的数量的输入,而这个在input的描述里面是有的,测试的时候也是有的
最后我是不想整了,因为我是有用到getchar函数,需要考虑换行符的位置啥的,下面的代码只实现了一个case的处理
因为题目要求”把终止队列的第一个放在等待队列的第一个”,所以这题可以用到deque,也就是双端队列,可以进行队首队尾元素的出入而保证效率
但是能不用的话就不用,因为在排序上的效率较低,甚至不如复制到vector然后排序的效率高
但是这题只有元素的进出,问题不大
代码如下:
#include<iostream>
#include<string>
#include<deque>
#include<queue>
#include<map>
using namespace std;
deque<int> waitDeq;//等待队列
queue<int> stopQue;//阻止队列
map<char, int> variable;//变量
map<int, queue<string>> program;//程序
int n, t1, t2, t3, t4, t5, Q;
bool lockState = false;
int main(void)
{
//输入
scanf("%d%d%d%d%d%d%d", &n, &t1, &t2, &t3, &t4, &t5, &Q); getchar();
for(int i = 1; i <= n; i++)
{
waitDeq.push_back(i); program[i] = queue<string>{};
while(true)
{
string s; getline(cin, s);
program[i].push(s);
if(s == "end") break;
}
}
//运行
while(!waitDeq.empty())
{
int ID = waitDeq.front(), time = 0; waitDeq.pop_front();
bool is_push = true;
while(time < Q)
{
if(program[ID].front() == "lock")
{
if(lockState){stopQue.push(ID); is_push = false; break;}
else{lockState = true; program[ID].pop(); time += t3;}
}
else if(program[ID].front() == "unlock")
{
lockState = false; time += t4; program[ID].pop();
if(!stopQue.empty()) waitDeq.push_front(stopQue.front());
stopQue.pop();
}
else if(program[ID].front() == "end")
{
is_push = false, time += t5;
break;
}
else if((program[ID].front())[2] == '=')
{
int value = 0;
for(int i = 4; i < program[ID].front().size(); i++)
value = value * 10 + (program[ID].front())[i] - '0';
variable[(program[ID].front())[0]] = value; time += t1;
program[ID].pop();
}
else
{
char c = (program[ID].front())[6];
if(variable.count(c)) printf("%d: %d\n", ID, variable[c]);
else printf("%d: 0", ID);
time += t2; program[ID].pop();
}
}
if(is_push) waitDeq.push_back(ID);
}
return 0;
}