uva210
- 程序循环进行 : 循环链表
- Lock未遂的程序依次抓走 : 队列
- 数据结构刚好看到循环链表,感觉写好了插入删除很方便
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
struct Node;
#define TEST 1
typedef Node *pNode;
typedef queue<string> Program;
struct Node
{
Node(int k, pNode p = NULL) : n(k), next(p) {}
int n;
pNode next;
};
pNode Create(int n)
{
pNode ret = new Node(-1);
pNode cur = ret;
for (int i = 0; i != n; ++i)
{
cur->next = new Node(i);
cur = cur->next;
}
cur->next = ret;
return ret;
}
pNode del(pNode h, pNode d)
{
while (h->next != d)
h = h->next;
h->next = d->next;
delete d;
return h;
}
void toback2(pNode curr, queue<int> &iq)
{
curr->next = new Node(iq.front(), curr->next);
iq.pop();
}
void run(ostream &);
int Q;
int Time[5];
int v[26];
int pro_cnt;
bool LOCK;
vector<Program> Kase;
int main()
{
ofstream ofs;
if (TEST)
{
ofs.open("/home/lixiaoqi/Documents/Code/C++/1.txt");
if (!ofs.is_open())
throw runtime_error("FILE NOT OPEN!");
}
ostream &os = TEST ? ofs : cout;
int N;
string s;
cin >> N;
getchar();
while (N--)
{
Kase.clear();
LOCK = false;
memset(v, 0, sizeof v);
cin >> pro_cnt;
for (int i = 0; i != 5; ++i)
cin >> Time[i];
cin >> Q;
Kase.resize(pro_cnt);
getchar();
for (int i = 0; i != pro_cnt; ++i)
while (getline(cin, s) && s != "end")
Kase[i].push(s);
run(os);
os << (N ? "\n" : "");
}
return 0;
}
void run(ostream &os)
{
queue<int> block;
pNode h = Create(pro_cnt);
pNode cur = h;
while (h->next != h)
{
if (cur == h)
cur = cur->next;
int left = Q;
int i = cur->n;
while (Kase[i].size() && left > 0)
{
string s = Kase[i].front();
if (s == "lock")
{
if (!LOCK)
{
left -= Time[2];
LOCK = true;
}
else
{
block.push(i);
cur = del(h, cur);
break;
}
}
else if (s == "unlock")
{
if (block.size())
toback2(cur, block);
LOCK = false;
left -= Time[3];
}
else if (s[0] == 'p' && s[1] == 'r')
{
os << i + 1 << ": " << v[s[6] - 'a'] << '\n';
left -= Time[1];
}
else
{
int k = 0;
for (int j = 4; j != s.size(); ++j)
k = k * 10 + s[j] - '0';
v[s[0] - 'a'] = k;
left -= Time[0];
}
Kase[i].pop();
}
if (Kase[i].empty())
cur = del(h, cur);
cur = cur->next;
}
}
|