您的位置:首页 > 其它

Sicily 1443

2014-11-08 20:53 218 查看
题目蛮好理解,就是简单的对队列进行操作。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);
}
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: