1966번 프린터 큐

1966번 프린터 큐 백준 알고리즘 문제

이 문제는 다름이 아니라, 문제를 이해하는 게 더 어려웠다. 일단 설명이 부족한 것 같아 문제는 좀 별로라고 생각되긴 한다.

문제 자체는 굉장히 단순하다. queue 를 잘 활용하면 간단하게 해결 가능하다.

// 프린터 큐 1966 번

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

int T;
int n, m;
int val;
queue<pair<int, bool>> q;

int arr[200];
int main() {
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      if (i == m)
        q.push(make_pair(arr[i], true));
      else
        q.push(make_pair(arr[i], false));
    }
    sort(arr, arr + n);
    int cnt = 0;
    for (int i = n - 1; i >= 0;) {
      if (q.front().first != arr[i]) {
        pair<int, bool> temp;
        temp = q.front();
        q.pop();
        q.push(temp);
        continue;
      } else {
        if (q.front().second) {
          cnt++;
          break;
        } else {
          q.pop();
          i--;
          cnt++;
        }
      }
    }
    while (!q.empty()) {
      q.pop();
    }
    cout << cnt << endl;
  }
  return 0;
}