Sicily 1443

来源:互联网 发布:笔记本触摸屏测试软件 编辑:程序博客网 时间:2024/06/06 10:40

题目蛮好理解,就是简单的对队列进行操作。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个位置的priorityint 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);}}}}}


0 0
原创粉丝点击