deque双端队列&&UVa210 Concurrency Simulator(并行程序模拟)的理解与解析
来源:互联网 发布:帝国cms整合ck 编辑:程序博客网 时间:2024/04/30 09:28
deque 双端队列
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of the input file consists of seven integers separated by spaces. These integers specify(in order): the number of programs which follow, the unit execution times for each of the five statements(in the order given above), and the number of time units comprising the time quantum. The remainder of the input consists of the programs, which are correctly formed from statements according to the rules described above.
All program statements begin in the first column of a line. Blanks appearing in a statement should be ignored. Associated with each program is an identification number based upon its location in the input data (the first program has ID = 1, the second has ID = 2, etc.).
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Your output will contain of the output generated by the print statements as they occur during the simulation. When a print statement is executed, your program should display the program ID, a colon,a space, and the value of the selected variable. Output from separate print statements should appear on separate lines.
Sample Input
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
Sample Output
1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21
Solution
#include <iostream>#include <deque>#include <queue>#include <vector>#include <string>#include <cstdio>#include <cstring> // memset(,,) 是在这里面的using namespace std;const int N = 1005;//这些变量都定义成全局,只要是函数里面要用到的东西比较多,总不能给函数那么多的参数吧,这么做挺好的bool lock;deque<int> qr;//等待队列 把每个队列,用给队列的序号来记录、处理queue<int> qb;//阻止队列vector<string> prg[N];string s;int t[N], p[N], var[26]/*可以用26个字母代替26个数字,因为变量最多只有26个*/, lim; //m[26]等,把二十六个字母转化为数字来进行储存和处理,很常用的void run(int i)//每一次执行配额时间(单位时间){ int rt = lim, v; //copy一份配额(单位时间),这样可以在使用的时候保留原来的标准lim配额 string cur; while(rt > 0){ cur = prg[i][p[i]]; if(cur[2] == '='){ // 赋值 rt -= t[0]; v = cur[4] - '0'; if(cur.size() == 6) v = v * 10 + cur[5] - '0';//!常数是小于100的数,所以最多是两位数。故.size()==6就是两位了 var[cur[0] - 'a'] = v; //!给变量赋值了 ,且把字母化为数字来存储了 } else if(cur[2] == 'i'){ //print rt -= t[1]; printf("%d: %d\n", i, var[cur[6] - 'a']); //打印的只会是单个的字母 所对应 的变量 的值 } else if(cur[2] == 'c'){ //lock rt -= t[2]; if(lock){ qb.push(i); //!queue用的.push() 而不用push_back() return; //直接进入下一个程序的运行 } else lock = true; } else if(cur[2] == 'l'){ //unlock lock = false; rt -= t[3]; if(!qb.empty()){ //阻止队列 v = qb.front(); //的第一个 qb.pop(); //程序进入 qr.push_front(v); } } else return; //end 如果到这里,说明它运行了 end 可以直接退出函数 ++p[i]; } qr.push_back(i); //main()里面有qr.pop_front(i)了,这样就把它放到末尾了}int main(){ int cas, n; scanf("%d", &cas); while(cas--){ scanf("%d", &n); for(int i = 0; i < 5; ++i) scanf("%d", &t[i]); scanf("%d", &lim); for(int i = 1; i <= n; ++i){ //队列名总不能从0开始 prg[i].clear(); while(getline(cin, s)){ if(s == "") continue; //i记录的是队列的序号 prg[i].push_back(s); if(prg[i].back() == "end") break;//.back() 和 .front() } qr.push_back(i); //这个不要漏掉 } memset(p, 0, sizeof(p)); memset(var, 0, sizeof(var)); while(!qr.empty()){ int cur = qr.front(); qr.pop_front(); run(cur); } if(cas) puts(""); } return 0;}//!@ProLights
- deque双端队列&&UVa210 Concurrency Simulator(并行程序模拟)的理解与解析
- Uva210 Concurrency Simulator 【双端队列deque】【例题6-1】
- UVA210 concurrency simulator (双向队列 & 模拟)
- 例题6-1 并行程序模拟 UVa210 双端队列
- 例题6-1 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991, UVa210)
- 210 - Concurrency Simulator【模拟、双端队列】
- UVa210 Concurrency Simulator
- UVa 210 - Concurrency Simulator(模拟+双端队列)
- UVA 210 Concurrency Simulator(deque,queue,模拟)
- uva 210 Concurrency Simulator (并行堆栈模拟)
- UVa 210 Concurrency Simulator(双端队列)
- UVA - 210 - Concurrency Simulator <双端队列>
- UVa 210 Concurrency Simulator(双端队列)
- UVa 210 Concurrency Simulator(双端队列)
- 【日常学习】【双端队列】 Uva - 210 Concurrency Simulator题解
- 例题6-1:并行程序模拟(deque的使用)
- 例题6-1 双端队列 uva210
- 并发与并行(concurrency and parallelism)的思考理解
- CGContextSaveGState与CGContextRestoreGState的作用 (转)
- this 用法之作用域:$("option:selected",this)
- 【程序3】该存多少钱 (逆推实例)
- 如何避免多控件窗体重新布局时闪烁
- 深入理解HTTP Session
- deque双端队列&&UVa210 Concurrency Simulator(并行程序模拟)的理解与解析
- 使用PHP搭建自己的MVC框架
- ASP显示对话框后页面跳转
- 第九周项目二对称矩阵的压缩存储及基本运算
- Uicc之ICCFileHandler(转)
- Activity启动模式(lauchMode)
- 2015南阳理工CCPC Sudoku
- 32位/64兼容 VC inline 汇编极限优化范例之颜色混合: AlphaBlendPixel
- 奇妙的NullPointerException