Legacy/Be Refactoring..

acmicpc.net / 강의無 - No.1966 - 프린터 큐

Marco 2020. 7. 21. 00:42
728x90

알고리즘 기초 1/2 - No.10799 - 쇠막대기

 

TODO

  1. 현재 Queue의 가장 앞에 있는 문서의 ‘중요도’를 확인한다.
  2. 나머지 문서들 중 현재 문서보다 중요도가 높은 문서가 하나라도 있다면, 이 문서를 인쇄하지 않고 Queue의 가장 뒤에 재배치 한다. 그렇지 않다면 바로 인쇄를 한다.

 

Constraints

첫줄에 test case N (100이하)

Test case 1) Queue의 크기 M (문서갯수 0이상 N미만)

Test case 2) Queue의 내용 (각 문서 중요도 1이상 9이하) 

 

Sample Input

3

1 0

5

4 2

2 3 4

6 0

1 1 9 1 1 1

 

Sample Output

1

2

5

 

#include <string>
#include <vector>
#include <queue>

#include <iostream>

using namespace std;

int main() {
    int total;
    cin>>total;

    for(int i=0; i<total; i++) {
        vector<int> priorities;
        int n, location, answer = 1;

        cin>>n;
        cin>>location;
        
        queue<int> q, temp;

        for(int i=0; i<n; i++) {
            int tmp;
            cin>>tmp;
            priorities.push_back(tmp);
            q.push(tmp);
        }

        while(1) {
            int front = q.front();
            temp = q;
            // 큐 복사물로 뒤에 우선순위 높은걸 찾기
            bool check = true;
            while(temp.size()>0) {     
                if(temp.front() > front) {  // 우선순위 높은게 존재
                    q.push(front);
                    q.pop();
                    location==0 ? location = q.size()-1 : location--;
                    check = false;
                    break;
                } else {                    // 우선순위 높은게 없음
                    temp.pop();
                }
            }

            if(check==true) {
                // 복사 큐가 끝날때까지 front 보다 우선 순위 높은게 없음(q 변동 없음) front 출력 
                q.pop();

                if(location==0) // 원하던 자료가 출력됨;
                    break;
                location--;
                answer++;
            }
        }

        cout<< answer <<"\n";
    }
    return 0;
}

How I tried this :

프로그래머스 - 프린터 

문제와 동일했다 단지 Test case 포문 안에 기존의 코드를 넣을뿐...

 

What I got is : 

프로그래머스 - 프린터 

 

I have to study :

Queue

 

 

문제에 관한 모든 저작권 : https://acmicpc.net
728x90