例题6-1 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991, UVa210)
2017-07-15 11:25
746 查看
#include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <sstream> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <cctype> #define CLOSE() ios::sync_with_stdio(false) #define CLEAR(a, b) memset(a, b, sizeof(a)) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) const int maxn = 100005; using LL = long long; using UI = unsigned int; using namespace std; //------------------------------------------------------------------------------------------// //STL实现。 vector<deque<string>> prog; int main() { #ifdef _DEBUG IN(); OUT(); #endif int T; scanf("%d", &T); while (T--) { int n, time[5], qutm; scanf("%d", &n); for (int i = 0; i < 5; ++i) scanf("%d", &time[i]); string s; scanf("%d", &qutm); getline(cin, s); prog.clear(); deque<int> ready; for (int i = 0; i < n; ++i) { ready.push_back(i); while (getline(cin, s)) { prog.push_back(deque<string>()); prog[i].push_back(s); //cout << s << endl; if (s == "end") break; } //cout << endl; } queue<int> blocked; map<char, int> vari; bool lock = false; while (!ready.empty()) { int nowprog = ready.front(); ready.pop_front(); ready.push_back(nowprog); //printf("now = %d\n", nowprog+1); int t = qutm; while (t > 0) { string inst = prog[nowprog].front(); prog[nowprog].pop_front(); int p = inst.find('='), p2 = inst.find("print"); if (inst == "end") { ready.pop_back(); break; } else if (p != -1) { vari[inst[p - 2]] = atoi(inst.substr(p + 2).c_str()); t -= time[0]; } else if (p2 != -1) { char va = inst[p2 + 6]; printf("%d: %d\n", nowprog+1, vari[va]); t -= time[1]; } else if (inst == "lock") { if (!lock) { lock = true; t -= time[2]; } else { ready.pop_back(); blocked.push(nowprog); prog[nowprog].push_front(string("lock")); break;//执行完后记得break } } else { t -= time[3]; lock = false; if (!blocked.empty()) { ready.push_front(blocked.front()); blocked.pop(); } } } } if (T) puts(""); } return 0; }
#include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <sstream> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <cctype> #define CLOSE() ios::sync_with_stdio(false) #define CLEAR(a, b) memset(a, b, sizeof(a)) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) const int maxn = 100005; using LL = long long; using UI = unsigned int; using namespace std; //------------------------------------------------------------------------------------------// vector<vector<string>> prog; vector<pair<int, int>> rf; int readyq[maxn], rrear, rfront; int blocked[maxn], brear, bfront; //自行实现双端队列 int main() { #ifdef _DEBUG IN(); OUT(); #endif int T; scanf("%d", &T); while (T--) { int n, time[5], qutm; scanf("%d", &n); for (int i = 0; i < 5; ++i) scanf("%d", &time[i]); scanf("%d", &qutm); getchar(); prog.clear(); rf.clear(); CLEAR(readyq, 0); rrear = rfront = 0; for (int i = 0; i < n; ++i) { readyq[rfront++] = i; string s; while (getline(cin, s)) { prog.push_back(vector<string>()); rf.push_back(make_pair(0, 0)); prog[i].push_back(s); //cout << s << endl; if (s == "end") break; } //cout << endl; } CLEAR(blocked, 0); brear = bfront = 0; map<char, int> vari; bool lock = false; while (rrear != rfront) { int nowprog = readyq[rrear++]; readyq[rfront++] = nowprog; //printf("now = %d\n", nowprog+1); int t = qutm; while (t > 0) { string inst = prog[nowprog][rf[nowprog].first++]; int p = inst.find('='), p2 = inst.find("print"); if (inst == "end") { rfront--; break; } else if (p != -1) { vari[inst[p - 2]] = atoi(inst.substr(p + 2).c_str()); t -= time[0]; } else if (p2 != -1) { char va = inst[p2 + 6]; printf("%d: %d\n", nowprog + 1, vari[va]); t -= time[1]; } else if (inst == "lock") { if (!lock) { lock = true; t -= time[2]; } else { rfront--; blocked[bfront++] = nowprog; rf[nowprog].first--; break; } } else { t -= time[3]; lock = false; if (brear != bfront) { readyq[--rrear] = blocked[brear++]; } } } } if (T) puts(""); } return 0; }
相关文章推荐
- UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列
- 习题7-1 消防车(Firetruck, ACM/ICPC World Finals 1991, UVa208)
- 例题6-14 Abbott的复仇(Abbott's Revenge, ACM/ICPC World Finals 2000, UVa 816)
- UVa816 例题 6-14 Abbott的复仇 (Abbott's Revenge,ACM/ICPC World Finals 2000)
- 信息解码(Message Decoding,ACM/ICPC World Finals 1991, UVa213)
- [UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码
- Message dicoding, ACM/ICPC World Finals 1991, UVa 213
- 信息解码(Message Decoding, ACM/ICPC World Finals 1991, UVa 213)
- UVA 213 ACM-ICPC World Finals 1991 信息解码
- 习题7-3 多米诺效应(The Domino Effect, ACM/ICPC World Finals 1991, UVa211)
- 例题6-19 自组合(Self-Assembly, ACM/ICPC World Finals 2013, UVa 1572)
- 例题6-1 并行程序模拟 UVa210 双端队列
- Sqaures, ACM/ICPC World Finals 1990, UVa
- [UVa 201]Squares 正方形,ACM/ICPC World Finals 1990
- UVA 210 并行程序模拟
- 算法竞赛入门经典-习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994,UVa232)
- 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
- 习题7-2 黄金图形(Golygons, ACM/ICPC World Finals 1993, UVa225)
- 习题7-4 切断圆环链(Cutting Chains, ACM/ICPC World Finals 2000, UVa818)
- 【DP 训练】Stamps and Envelope Size, ACM/ICPC World Finals 1995, UVa242