UVA - 12100 Printer Queue

/* 
  法一:
  这题最开始时,想到可能需要用优先队列...可是还是懵懵懂懂,不知道怎么下手,后来看到了法一的思路,发现博主就只是用到普通队列,思路如下:
  
  任务依次入队,压入的数为其优先级
  
  进入最外层 while(1)循环,该循环直到所关注的任务刚好位于队首,且打印结束后,退出while(1)循环
  每次取出队列中第一个元素,若:
  1. 如果队列中有优先级比队首更大的元素,则将队首移动到队尾,并更改所关注任务的序号(除非被关注的就是第一个,序号变为队尾对应的下标;否则,序号自减)
  
  2.目前的队首就是当前优先级最高的任务,删除队首并自增cnt。直到我们关注的任务就是队首(m == 0,下标为0时,关注元素变队首),这时我们关注的任务已被删除,可直接输出cnt
  
  3.如果刚刚删除的队首不是所关注元素,则回到1

  
  法一借鉴思路于: http://blog.csdn.net/a197p/article/details/43724037

*/



#include <iostream>
#include <vector>
using namespace std;

vector<int>queue;

int main()
{
	int t, n, x, m;
	cin >> t;
	while (t--)
	{
		int cnt = 0;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			queue.push_back(x);
		}
		while (1)
		{
			for (int i = 0; i < (int)queue.size(); i++)
			{
				if (queue[i] > queue[0])
				{
					queue.push_back(queue[0]);
					queue.erase(queue.begin());
					if (m) m--;
					else m = (int)queue.size() - 1;
					i = 0; //此处千万不要漏掉!!!该语句的含义为:若队首元素不是当前队伍中优先级最大的,队首移动到最后,i重新置为0,是为了检验,原队首的后一个元素,是不是队中优先级最大的,如此循环,直到队首变为队中优先级最大的 
				}
				
				
			}
			
			queue.erase(queue.begin());
			cnt++;
			
			if (m) m--;
			else
			{
				cout << cnt << endl;
				break;
			}
		}	
		queue.clear();

	}
	return 0;
}


/*
  法二:
  法二的改进之处在于:用了结构体,并用一个数组来记录优先级;
  
  法二参考自blog: http://blog.csdn.net/mistkafka/article/details/9343759
  同时这个blog也解释了,为什么不能只用一个优先队列完成此题
  但如果想用优先队列,可以再用一个普通队列,两者结合起来,也可解决该问题(见法三)
 
  此外,sort函数如果是要降序排列内置类型,不必重写比较函数,见下:
  http://blog.csdn.net/zhinanpolang/article/details/50917019  
  
  BTW:
  我自己敲一次的时候,由于我的疏忽,导致调试时还出现了“段错误”,最后发现,是本来该写 (i == m)的地方,被我粗心写为了(pri[i] == m)
  这是我第一次遇到段错误的情况,有关段错误的介绍,找到了一篇详细的博客,一并附上:
  http://www.cnblogs.com/hello--the-world/archive/2012/05/31/2528326.html

*/

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 105;

struct Node
{
	int pri; //priority
	bool tar; // if is target
	
	Node(int p = 0, bool t = false) : pri(p), tar(t)
	{
	}
};

int main()
{
	int t, n, m;
	cin >> t;
	while (t--)
	{
		queue<Node> que;
		int pri[N]; //job_priority
		
		while(!que.empty()) que.pop();	
		
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			cin >> pri[i];
			if (i == m) que.push(Node(pri[i], true));
			else que.push(Node(pri[i], false));
		} 
		
		sort(pri, pri+n, greater<int>());
		
		bool out = false; // 判断是否可退出循环 
		int cnt = 0; // 记录工作时间 
		int *p = pri; // 指针,指向当前的最高优先级
		
		while (!out)
		{
			Node tp = que.front();
			que.pop();
			
			if (tp.pri == *p) //如果队首的任务优先级为全队最高
			{
				cnt++; p++; //别忘了移动指针,使得下一个出队的,变为次高优先级的任务,循环直到队首优先级最高,且队首为目标任务 
				if (tp.tar) out = true;
			}
			else
			que.push(tp);
		}
		cout << cnt << endl;
	}
	return 0;
} 


/*
  法三;
    法三就是优先队列的做法了(并非不可用,而是不能只用一个优先队列完成此题),不过,如果想用优先队列,题目中必须用上两个队列,一个普通,一个优先
	
	参考自blog: http://blog.csdn.net/shihongliang1993/article/details/73550726
*/
#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int t, n, m, x, target; // target记录我们所关注的任务的优先级 
	cin >> t;
	while (t--)
	{
		int cnt = 0;
		queue<int> Q;
		priority_queue<int> PQ;
		cin >> n >> m;
		
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			if (i == m) target = x;
			Q.push(x);
			PQ.push(x);
		}
		
		while (!PQ.empty())
		{
			while (PQ.top() != Q.front())
			{
				Q.push(Q.front());
				Q.pop();
				m = (m == 0 ? (int)Q.size() - 1 : m - 1);
			}
			
			PQ.pop();
			Q.pop();
			cnt++; m--;
			if (m < 0) break;
		}
		cout << cnt << endl;
	}
	return 0;
} 




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值