#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(int argc, char const *argv[])
{
int tcase, n, m, nums, cnt;
cin >> tcase;
for (int i = 0; i < tcase; i++)
{
cnt = 0;
queue<pair<int, int> > q;
priority_queue<int> pq;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> nums;
q.push(make_pair(i, nums));
pq.push(nums);
}
while (!pq.empty())
{
int nowidx = q.front().first;
int nowval = q.front().second;
q.pop();
if (nowval == pq.top())
{
pq.pop();
cnt++;
if (nowidx == m)
{
cout << cnt << endl;
break;
}
}
else
{
q.push(make_pair(nowidx, nowval));
}
}
}
return 0;
}
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(int argc, char const *argv[])
{
int tcase, n, m, nums, cnt;
cin >> tcase;
for (int i = 0; i < tcase; i++)
{
cnt = 0;
queue<pair<int, int>> q;
priority_queue<int> pq;
cin >> n >> m;
int tmp;
for (int i = 0; i < n; i++)
{
cin >> nums;
if (i == m)
tmp = nums;
pq.push(nums);
}
while (!pq.empty())
{
cnt++;
if (tmp == pq.top())
{
if (!pq.empty())
{
pq.pop();
continue;
}
}
pq.pop();
}
cout << cnt << endl;
}
return 0;
}
큐에도 pair
구조를 넣어서 사용할 수 있다는 것을 알게되었다. 🙂