uva 210

原题

常规但是有点烦的一道模拟队列题..

一开始理解错题意了,导致后来思路有点乱, 换了好几次容器

因为program循环执行的顺序是不一定的, 所以不能采用遍历一个program数组更新状态的方法

而应该还是采用消息队列的方法, 每执行完一个program, 如果program还有命令没执行完而且没有被block就继续往队尾投递一个消息

注意题目中的block queue, 每次unlock之后, block queue中的program都要跑到队头, 这里用queue就很麻烦了, 所以一开始就应该用deque

所以每次unlock都要检查block queue是否有program, 有则投递一个消息到队头

最坑的是, 一开始没看到题目说的是两个test case之间要有空行, 就写成每运行完一个case就输出一个空行了

结果不断WA.. 最后还是查了答案才发现, 最后一个case运行完不能有空行, 否则就要WA了.. 坑爹的格式

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
const int MAXN 	= 100000;
const int BASE 	= 100000000;
typedef long long LL;

/*
uva 210

*/
struct program{
	deque<string> cmd;
	bool inBlock;
};

vector<program> p;
map<string,int> cost;
deque<int> dq;

int var[26];
int assign, output, lock, unlock, stop, quantum;
int inLock;
int N,T;
char buf[3][10];
bool isEnd = false;

void Pop(int i){
	memset(buf,0,sizeof(buf)); 
	char tmp[20];
	if( !p[i].cmd.empty() ){
		strcpy(tmp,p[i].cmd.front().c_str());
		p[i].cmd.pop_front();
		int j = 0;
//		cout << tmp << endl;
		char * ptr = strtok(tmp," ");
		while( ptr!=NULL ){
			memcpy(buf[j++],ptr,strlen(ptr));
			ptr = strtok(NULL," ");
		}
	}
	return ;
}

void simulate(){
	queue<int> bq;
	while( !dq.empty() ){
		int i = dq.front();
		dq.pop_front();
		int q = 0;
		while( q<quantum && p[i].cmd.size()>0 && !p[i].inBlock ){
			Pop(i);										// 取出一条command 
			if( buf[1][0]=='=' ){
				q += cost["="];
				var[buf[0][0]-'a'] = atoi(buf[2]);
			}else{
				q += cost[buf[0]];
				if( strcmp(buf[0],"print")==0 ){
					cout << i+1 << ": ";
					cout << var[buf[1][0]-'a'] << endl ;
				}else if( strcmp(buf[0],"lock")==0 ){
					if( inLock==-1 ){
						inLock = i;
					}else{
						p[i].inBlock = true;
						bq.push(i);
					}
				}else if( strcmp(buf[0],"unlock")==0 ){
					if( !bq.empty() ){
//							cout << bq.front() << endl;
						dq.push_front(bq.front());
						p[bq.front()].inBlock = false;
						p[bq.front()].cmd.push_front("lock");
						bq.pop();
					}
					inLock = -1;
				}
			}
		}
		if( !p[i].cmd.empty() && !p[i].inBlock ) dq.push_back(i); 
	}
	isEnd = true;
	for(int i=0; i<N; i++){
		if( !p[i].cmd.empty() ) isEnd = false;
	}
}

void test(){
	bool isOver = false;
	for(int i=0; i<N; i++){
		printf("%-12d",i+1);
	}
	cout << endl;
	int line = 0;
	while( !isOver ){
		isOver = true;
		for(int i=0; i<N; i++){
			if( line<p[i].cmd.size() ){
				cout << setw(12) << left << p[i].cmd.at(line);
				isOver = false;
			}else{
				printf("%-12s"," ");
			}
		}
		line++;
		printf("\n");
	}
}

int main(){
//	freopen("input2.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	
	cin >> T;
	while( T-- ){
		string cmd;
		p.clear();
		dq.clear();
		cost.clear();
		inLock = -1;
		isEnd = false;
		memset(var,0,sizeof(var));
		cin >> N >> assign >> output >> lock >> unlock >> stop >> quantum;
		cost["="] = assign;
		cost["print"] = output;
		cost["lock"] = lock;
		cost["unlock"] = unlock;
		cost["end"] = stop;
		
		for(int i=0; i<N; i++){
			program tmp;
			do{
				getline(cin,cmd);
//				cout << cmd << endl;
				if( cmd.length()>0 ) tmp.cmd.push_back(cmd);
			}while( cmd!="end" );
			p.push_back(tmp);
			dq.push_back(i);
		}
//		test();
		simulate();
		if( T ) cout << endl;
	}
	
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值