(队列的应用5.3.3)POJ 3125 Printer Queue(优先队列的使用)

/*
 * POJ_3125.cpp
 *
 *  Created on: 2013年10月31日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		queue<int> q;
		priority_queue<int> v;

		int n, m;
		scanf("%d%d", &n, &m);

		int i;
		for (i = 0; i < n; ++i) {
			int a;
			scanf("%d", &a);

			q.push(a);
			v.push(a);
		}

		while (true) {
			int x = q.front();
			q.pop();

			if (m == 0) {//如果m==0,则证明现在打印的是目标任务
				if (x != v.top()) {//如果队列中还有优先级比x高的..
					m = v.size() - 1;//下标是从0开始的
					q.push(x);//将该任务放到队尾
				} else {
					break;
				}
			} else {//如果现在的任务还不是目标任务
				--m;
				if (x != v.top()) {
					q.push(x);
				} else {
					v.pop();
				}
			}

		}

		printf("%d\n", n - q.size());
	}

	return 0;
}


posted on 2013-10-31 21:21  新一  阅读(246)  评论(0编辑  收藏  举报

导航