티스토리 뷰

https://www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

const fs = require('fs');
const [n, ...arr] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const N = +n;
const docs =[];

for(let i = 0; i<N; i++){
    const [num,target] = arr.shift().split(' ');
    const priority = arr.shift().split(' ');
    docs.push([num,target,priority]);
}

class Node{
    constructor(item){
      this.item = item;
      this.next = null;
    }
  }
  
  class Queue{
    constructor(){
      this.head = null;
      this.tail = null;
      this.length = 0;
    }
  
    push(item){
      const node = new Node(item)
      if(this.head===null){
        this.head= node;
        this.head.next = null;
      }else{
        this.tail.next = node;
      }
  
      this.tail = node;
      this.length +=1;
    }
  
    pop(){
        if(this.empty()==1) return -1
      const popItem = this.head;
      this.head = this.head.next;
      this.length -=1;
      return popItem.item;
    }
  
    size(){
      return this.length;
    }
  
    empty(){
      if(this.size()==0){
        return 1;
      }else{
        return 0;
      }
    }
  
    front(){
      if(this.empty()==1) return -1;
      return this.head.item; 
    }
  
    back(){
      if(this.empty()==1) return -1;
      return this.tail.item; 
    }
  }
  




let answer = [];
docs.forEach(v=>{
    const [num,target,priority] = v;
    let q = new Queue();
    let priorityArr = [0,0,0,0,0,0,0,0,0,0];
    for(let i = 0; i<num; i++){
        q.push({index:i,priority:priority[i]});
        priorityArr[priority[i]]++;
    }
    

    function check(p){
        if(p==9) return true;
        for(let i = 9; i>p; i--){
            if(priorityArr[i]>0) return false;
        }
        return true;
    }
    let cnt = 0;
    while(true){
        
        let temp = q.pop();
        if(check(temp.priority)){
            cnt++;
            if(temp.index==target){
                answer.push(cnt)
                break; 
            }else{
                priorityArr[temp.priority]--; 
            }
        }else{
            q.push(temp);
        }
        
    }
})

console.log(answer.join('\n'))
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함