#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
// 문서의 중요도가 순서대로 담긴 동적배열 priorities
// 인쇄를 요청한 문서의 대기목록의 위치 location
int solution(vector<int> priorities, int location)
{
// location에 있는 요청 문서가 몇 번째로 인쇄되는가?
int answer = 0;
queue<pair<int, int>> que; // key:value
// 들어온 문서의 양만큼 큐에 저장한다. 이때, 순번과 중요도를 저장한다.
for (int i = 0; i < priorities.size(); i++)
{
// (A, 2) -> (B, 1) -> (C, 3) -> (D, 2) 순서로 삽입
que.push(make_pair(i, priorities[i]));
}
// 문서 정렬
sort(priorities.begin(), priorities.end());
// que는 현재 4번 삽입한 것을 이론상으로 알고 있다.
// 4 가지의 데이터를 차례대로 삭제해서 하나도 없을때까지 반복한다.
while(!que.empty())
{
// 오름차순으로 정렬시켰기 때문에 가장 높은 중요도를 가진 항목이 맨 뒤에 있다.
int top = priorities.back(); // 우선순위 높은 값 대입
pair<int, int> temp; // 스왑 용도
while(1)
{
// 현재 맨 앞의 que의 우선순위가 제일 높은 중요도 수치라면
if (que.front().second == top)
{
// 순번 증가
answer++;
// 문서의 순서가 인쇄 순서와 같다면
if (que.front().first == location)
{
// 현재까지 증가된 순번 리턴
return answer;
}
// 현재 큐 삭제
que.pop();
break;
}
// 대기 목록에 중요도가 높은 문서가 있는 경우
else
{
temp = que.front(); // 현재 큐를 temp에 삽입
que.pop(); // 현재 큐 삭제
que.push(temp); // 마지막으로 순서 이동(삽입)
}
}
priorities.pop_back();
}
return answer;
}