Uva 12100 Printer Queue

题目:Printer Queue


题意:有n个打印任务,对于每个任务,如果有比它重要的任务在后面,就把它排到队伍的末尾。带引每个任务需要1的时间,把任务插在末尾不需要时间。问打印完任务m需要多少是时间。


思路:用队列存储,模拟。每一个任务要记下它的重要度和编号。


关于STL的queue:


百度百科中关于queue成员函数的介绍——

q.empty()判断队列q是否为空,当队列q空时,返回true;否则为false(值为0(false)/1(true))。
q.size()访问队列q中的元素个数。(不可写成sizeof(q)或size(q))
q.push(a)会将一个元素a置入队列q中。
q.front()会返回队列q内的第一个元素(也就是第一个被置入的元素)。(不可写成front(q))
q.back()会返回队列q中最后一个元素(也就是最后被插入的元素)。(不可写成back(q))
q.pop()会从队列q中移除第一个元素。(不可写成pop(q))[1] 

queue没有 .find()函数,也不能用迭代器进行遍历,所以我对于查找是否有更高的重要度时用了一个map来记录。

清空一个queue时,不能像其他结构一样 .clear(),我的做法是定义一个空的queue,再让需要清空的queue等于空的queue。


代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

struct Pair {
	int x,id;
	Pair() {}
	Pair(int one,int two) {
		x=one,id=two;
	}
	bool operator <(const Pair other) const{
		if(x<other.x||(x==other.x&&id<other.id)) return true;
		return false;
	}
};

int n,m;
queue<Pair> que;
map<Pair,bool> mp;

bool judge(int i){
	for(map<Pair,bool>::iterator it=mp.begin();it!=mp.end();it++){
		Pair x=(it->first);
		bool y=(it->second);
		if(y==true&&x.x>i) return false;
	}
	return true;
}

int check(int f) {
	int j=0;
	while(!que.empty()) {
		Pair head=que.front();
		que.pop();
		if(!judge(head.x)) {
			que.push(head);
		} else if(head.id==f) {
			return ++j;
		} else {
			++j;
			mp[head]=false;
		}
	}
	return false;
}

void init(){
	mp.clear();
	queue<Pair> t;
	que=t;
}

int main() {

	int T;
	scanf("%d",&T);
	while(T--) {
		init();
		scanf("%d%d",&n,&m);
		for(int i=0; i<n; i++) {
			int x;
			scanf("%d",&x);
			que.push(Pair(x,i));
			mp[Pair(x,i)]=true;
		}
		printf("%d\n",check(m));
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值