注:c++实验课堂题目
Input
One line with a positive integer: the number of test cases (at most 100). Then for each test case:
One line with two integers n and m, where n is the number of jobs in the queue (1 ≤ n ≤ 100) and m is the position of your job (0 ≤ m ≤ n −1). The first position in the queue is number 0, the second is number 1, and so on.
One linewith n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The first integer gives the priority of the first job, the second integer the priority of the second job, and so on.
Output
For each test case, print one line with a single integer; the number of minutes until your job is completely printed, assuming that no additional print jobs will arrive.
Sample Input
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
Sample Output
1
2
5
// 用vector实现
// 注意善用node使得数组能够存储多个信息,这些信息其实包含在node中
#include<iostream>
#include<vector>
using namespace std;
struct node {
int index;
int priority;
node(int i = 0, int p = 0):index(i), priority(p) {};
};
int main() {
int t;
cin >> t;
while (t--) {
vector<node> que;
int alljob, myjob;
cin >> alljob >> myjob;
int temp;
for (int i = 0; i < alljob; ++i) {
cin >> temp;
que.push_back(node(i, temp));
}
int result = 0;
while (!que.empty()) {
node top = que.front();
bool flag = false;
for (int i = 1; i < que.size(); ++i) {
if (que[i].priority > que[0].priority) {
que.push_back(top);
flag = true;
break;
}
}
que.erase(que.begin());
if (flag == false) result++;
if (flag == false && top.index == myjob) break;
}
cout << result << endl;
}
return 0;
}