STL队列模拟。有无优先任务可以在读入时,直接对不同优先级的任务计数,然后得到当前的最高优先级直接判断。目标任务直接+10标记了。
#include <iostream> #include <cstring> #include <queue> using namespace std; queue<int> tasks; int main() { int kase, n, m, cur, taskCnt[10], curPri, t; cin >> kase; while (kase--) { tasks = queue<int>(); curPri = 10; t = 0; memset(taskCnt, 0, sizeof(taskCnt)); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> cur; taskCnt[cur]++; tasks.push(cur + (i == m? 10: 0)); } while (curPri > 1 && taskCnt[--curPri] == 0); while (!tasks.empty() && curPri > 0) { cur = tasks.front(); tasks.pop(); if (cur % 10 < curPri) { tasks.push(cur); } else { t++; // printed if (cur > 10) break; if (--taskCnt[cur] <= 0) while (curPri > 1 && taskCnt[--curPri] == 0); } } cout << t << endl; } return 0; }