Sicily 1443

题目蛮好理解,就是简单的对队列进行操作。ac后看了别人的代码,发现可以先把每个Job设置成一个struct,里面记录当前的位置和优先级。我直接在while循环里面进行判断了。所以看起来比较冗余。

题目:http://www.soj.me/1443

代码:

// Copyright <lijiancheng> [2014]
// Sicily 1443

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

int main () {
	int testcase;
	cin >> testcase;
	while (testcase--) {
		int n, m, ans = 1;
		deque<int> q;
		cin >> n >> m;

		// 获得第m个位置的priority
		int prio = 0;
		for (int i = 0; i < n; i++) {
			int temp;
			cin >> temp;
			q.push_back(temp);
			if (i == m) prio = temp;
		} 
		// 每次遍历遍历一遍 找最大 若第一个是最大则直接pop出去,否则进行移位

		int pos = m;
		while (1) {
			int maxPrio = 0;
			int maxPos = 0;
			deque<int>::iterator it = q.begin();
			for (int i = 0; it != q.end();i++, it++) {
				if (*it > maxPrio) {
					maxPrio = *it;
					maxPos = i;
				}
			}
			if (maxPos == 0) {
				if (pos == 0) {
					cout << ans << endl;
					break;
				}
				else {
					ans++;
					q.pop_front();
					pos--;
				}
			}
			else {
				if (pos == 0) {
					int temp = q.front();
					q.pop_front();
					q.push_back(temp);
					pos = q.size()-1;
				}
				else {
					pos--;
					int temp = q.front();
					q.pop_front();
					q.push_back(temp);
				}
			}
		}
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值