(백준) 싸지방에간 준하 _ 12764

2021. 12. 12. 23:05·알고리즘/백준

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

 

🐢 설명

싸지방 컴퓨터를 사용하는 순서가 주어졌을 때 최소 몇개의 컴퓨터가 있어야 대기없이 컴퓨터를 사용할 수 있는지 구하는 문제이다.

이때 사용가능한 컴퓨터 중에서 번호가 가장 빠른 컴퓨터 사용한다는 점이 중요하다.

 

1. 모든 컴퓨터의 이용시간을 시작 시간을 기준으로 user_우선순위큐에 넣는다.

2. 사용하는 컴퓨터를 담는 usingCom_우선순위큐에는 끝나는 시간을 기준으로 담는다.

2-1. 만약 usingCom이 없다면 컴퓨터 자리를 새로 만들어서 추가한다.

2-2. 만약 usingCom이 있다면 자신의 시작 시간과 컴퓨터의 사용가능한 시간대를 확인하고 가능한 컴퓨터의 자리는 좌석순서_우선순위큐에 담는다.

2-2-1. 좌석순서_우선순위큐가 비어있다면 시간대가 맞는 컴퓨터가 없고 전부 사용중이라는 뜻이므로 새로 추가한다.

2-2-2. 그렇지 않다면 하나를 뽑아서 좌석번호를 인덱스로 사용횟수를 늘려주고, usingCom 큐에 시작시간-끝시간 쌍을 넣어준다.

 

🐢코드
package Gold이상문제_정리;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class 싸지방에간준하_12764 {
    static int[] arr = new int[100001];
    static int N;
    static PriorityQueue<Computer> usingCom = new PriorityQueue<>(Comparator.comparing(c -> c.end));
    static PriorityQueue<Computer> user = new PriorityQueue<>(Comparator.comparing(c -> c.start));
    static PriorityQueue<Integer> remainSeat = new PriorityQueue<>();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        int seat = 0;

        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            user.add(new Computer(start, end));
        }

        while (!user.isEmpty()) {
            Computer userTime = user.poll();
            int start = userTime.start;
            int end = userTime.end;

            // no computer
            if (usingCom.isEmpty()) {
                usingCom.add(new Computer(++seat, start, end));
                arr[seat]++;
            }
            // find usable computer
            else {
                while (!usingCom.isEmpty()) {
                    Computer com = usingCom.peek();

                    // 사용가능
                    if (com.end < start) {
                        remainSeat.add(com.number);
                        usingCom.poll();
                    } else if (com.end > start) {
                        break;
                    }
                }

                // 사용가능한 컴퓨터 없음
                if (remainSeat.isEmpty()) {
                    usingCom.add(new Computer(++seat, start, end));
                    arr[seat]++;
                }
                // 사용가능한 컴퓨터 있음
                else {
                    int seatNum = remainSeat.poll();
                    arr[seatNum]++;
                    usingCom.add(new Computer(seatNum, start, end));
                }
            }
        }

        StringBuilder answer = new StringBuilder();
        answer.append(seat).append("\n");
        for (int i = 1; i <= seat; i++) {
            answer.append(arr[i]).append(" ");
        }
        System.out.println(answer);
    }

    private static class Computer {
        int number;
        int start;
        int end;

        public Computer(int number, int start, int end) {
            this.number = number;
            this.start = start;
            this.end = end;
        }

        public Computer(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
}
🐢 마무리
반응형
저작자표시 비영리 변경금지 (새창열림)

'알고리즘 > 백준' 카테고리의 다른 글

(백준) 구간나누기2 _ 13397  (0) 2021.12.22
(백준) 일감호에 다리 놓기 _ 17490  (1) 2021.12.20
(백준) 로봇 시물레이션 _ 2174  (3) 2021.12.10
(백준) 불 _ 5427  (0) 2021.12.08
(백준) 뱀 _ 3190  (0) 2021.10.22
'알고리즘/백준' 카테고리의 다른 글
  • (백준) 구간나누기2 _ 13397
  • (백준) 일감호에 다리 놓기 _ 17490
  • (백준) 로봇 시물레이션 _ 2174
  • (백준) 불 _ 5427
구름뭉치
구름뭉치
구름의 개발일기장
    반응형
  • 구름뭉치
    구름 개발일기장
    구름뭉치
  • 전체
    오늘
    어제
    • ALL (288) N
      • 프로젝트 (23)
        • 토스페이먼츠 PG 연동 시리즈 (12)
        • JWT 방식 인증&인가 시리즈 (6)
        • 스우미 웹 애플리케이션 프로젝트 (1)
        • 스프링부트 기본 보일러 플레이트 구축 시리즈 (2)
        • 마이크로서비스 아키텍쳐 시리즈 (1)
      • 스프링 (43)
        • 스프링부트 API 설계 정리 (8)
        • 스프링부트 RestAPI 프로젝트 (18)
        • 스프링부트 WebSocket 적용기 (3)
        • 스프링 JPA 정리 시리즈 (5)
        • 스프링 MVC (5)
        • 스프링 배치 (2)
        • 토비의 스프링 정리 (2)
      • 기술 학습 (4) N
        • 아파치 카프카 (9)
        • 클린 코드 (4)
        • 디자인 패턴의 아름다움 (2)
        • 모던 자바 인 액션 (7)
        • JVM 스레드 딥다이브 (7)
      • Web (25)
        • 정리글 (20)
        • GraphQL 정리글 (2)
        • Jenkins 정리글 (3)
      • 취업 (6)
      • CS (77)
        • 네트워크 전공 수업 정리 (11)
        • OSI 7계층 정리 (12)
        • 운영체제 정리 (19)
        • 데이터베이스 정리 (5)
        • MySql 정리 (17)
        • GoF의 Design Pattern 정리 (12)
      • 알고리즘 (70)
        • 백준 (56)
        • 프로그래머스 (12)
        • 알고리즘 정리본 (1)
      • 기초 지식 정리 (2)
      • 일상 (8)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    마우스 패드
    키보드 손목 받침대
    마우스
    류블라냐
    레이저
    부다페스트
    mx master s3 for mac
    크로아티아
    동유럽
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
구름뭉치
(백준) 싸지방에간 준하 _ 12764
상단으로

티스토리툴바