习题5-7 打印队列(Printer Queue, UVa12100)

习题5-7 打印队列(Printer Queue, UVa12100)

1.adding and removing jobs from the queue is instantaneous.
#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 <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 = 1e5 + 5;
using LL = long long;
using UI = unsigned int;
using namespace std;
//------------------------------------------------------------------------------------------//
struct A {
	int id, pri;
	bool operator<(const A &rhs) {
		return pri > rhs.pri;
	}
}a[105];

int main() {
	int T;
	//IN(); OUT();
	scanf("%d", &T);
	int n, tgt;
	while (T--) {
		scanf("%d%d", &n, &tgt);
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i].pri);
			a[i].id = i;
		}
		queue<A> q;
		int t = 0;
		for (int i = 0; i < n; i++) q.push(a[i]);
		sort(a, a + n);
		int pos; for (pos = 0; a[pos].id != tgt; pos++);
		int cnt = 0;
		while (1) {
			A x = q.front();//返回引用
			q.pop(); //printf("x.id = %d\n", x.id);
			if (q.empty()) { t++; break; }
			if (x.pri != a[cnt].pri) {
				q.push(x);
			}
			else {
				t++;
				cnt++;//printf("pop(%d)\n", x.id);
				if (x.id == tgt) break;
			}
		}
		printf("%d\n", t);
	}
	return 0;
}

/*
 deque iterator not dereferencable :
当队列为空时,进行front(),也会产生这种错误,
所以,在进行front时,首先判断是否empty(),如果emtpy返回一个错误的值,就可以了。
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值