백준
          
[1966] 프린터 큐
          
            by Jcoder
            2018. 7. 22.
            
          
        
       
      
        
            
            
#include <iostream>
#include <queue>
using namespace std;
int main()
{
	int i, testcase;
	cin >> testcase;
	for (i = 0; i < testcase; i++)
	{
		queue<pair<int, int>> q;
		priority_queue <int> pq;
		int n, m, cnt = 0;
		cin >> n >> m;
		for (int j = 0; j < n; j++)
		{
			int a;
			cin >> a;
			q.push({ j, a });
			pq.push(a);
		}
		while (!q.empty())
		{
			int index = q.front().first;
			int val = q.front().second;
			q.pop();
			if (pq.top() == val)
			{
				pq.pop();
				cnt++;
				if (index == m)
				{
					cout << cnt << endl;
					break;
				}
			}
			else
				q.push({ index, val });
		}
	}
	return 0;
}