记录一下每个priority中所占的人数。这样可以在遍历到第一个最大priority的文件时停止。
#include <iostream>
#include <cmath>
#include <ctime>
#include <climits>
#include <iomanip>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <iterator>
#include <queue>
using namespace std;
struct File
{
int ID, priority;
File(int ID = -1, int priority = 1) : ID(ID), priority(priority) {}
};
int prior[10];
queue<File> q;
int iterate(int target)
{
File out;
int min = 0;
while (out.ID != target)
{
int max_prior = 1;
for (int i = 9; i >= 1; i--)
if (prior[i] > 0)
{
max_prior = i;
break;
}
prior[max_prior]--;
while (q.front().priority != max_prior)
{
q.push(q.front());
q.pop();
}
out = q.front();
q.pop();
min++;
}
return min;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
{
memset(prior, 0, sizeof(prior));
while (!q.empty())
q.pop();
int m, n;
cin >> n >> m;
File f;
for (int i = 0; i < n; i++)
{
cin >> f.priority;
prior[f.priority]++;
f.ID = i;
q.push(f);
}
cout << iterate(m) << endl;
}
//end of program
return 0;
}