ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 2461๋ฒˆ - ๋Œ€ํ‘œ ์„ ์ˆ˜

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 6. 5. 21:20

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

 

2461๋ฒˆ: ๋Œ€ํ‘œ ์„ ์ˆ˜

์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ํ•™๊ธ‰์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N๊ณผ ๊ฐ ํ•™๊ธ‰์˜ ํ•™์ƒ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” M์ด ํ•˜๋‚˜์˜ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๋‹จ, 1 ≤ N, M ≤ 1,000์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ํ•œ

www.acmicpc.net

[๋ฌธ์ œ ํ’€์ด]

์ฒ˜์Œ์—” ๋‹จ์ˆœ ์กฐํ•ฉ์„ ํ†ตํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ•˜์˜€์ง€๋งŒ, ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ์ธํ•ด ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

 

๊ฐ๊ฐ์˜ ๋ฐ˜์˜ ์„ ์ˆ˜์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ์šฐ์„  ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์ฒ˜๋Ÿผ ๊ฐ ๋ฐ˜์˜ index 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ, 3๊ฐœ์˜ ๋ฐ˜์ค‘ ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ˜์˜ index๋ฅผ ์˜ค๋ฅธ์ชฝ 1 ์ด๋™์‹œํ‚ค๋ฉฐ ์ฐจ์ด๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

 

12      12     16     16     16      43     43      67      67      67
7   ->  17  -> 17 ->  17  -> 17  ->  17  -> 48  ->  48  ->  68  ->  68
14      14     14     15     54      54     54      54      54      77

 

๋งˆ์ง€๋ง‰์— 1๋ฐ˜ : 67 , 2๋ฐ˜ : 68 , 3๋ฐ˜: 77 ์ผ๋•Œ, ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ง€๋Š” 1๋ฐ˜์˜ ๊ฐ’์ด ๋”์ด์ƒ ์—†์œผ๋ฏ€๋กœ ๊ณ„์‚ฐ์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๊ตฌํ•ด์ง„ ์ฐจ์ด๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

- ์ตœ์†Œ๊ฐ’์„ ๊ณ„์† ๋ฝ‘์•„๋‚ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

- ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ๋ฐ˜์˜ index 0์„ ๋„ฃ์–ด ์ดˆ๊ธฐํ™” ํ•œ ํ›„, poll ํ•œ ๊ฐ’์˜ ๋ฐ˜์—์„œ ๋‹ค์Œ index๊ฐ’์„ ์šฐ์„ ์ˆœ์œ„์— ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

[์ •๋‹ต ์ฝ”๋“œ]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Student implements Comparable<Student>{
    int index = 0;
    int amount = 0;
    public Student(int index, int amount) {
        this.index = index;
        this.amount = amount;
    }

    @Override
    public int compareTo(Student o) {
        return amount - o.amount;
    }
}

public class Main {
    public static int N;
    public static int M;
    public static PriorityQueue<Integer>[] student;
    public static PriorityQueue<Student> pq = new PriorityQueue<>();
    public static int min = Integer.MAX_VALUE;
    public static int max = 0;
    public static int answer = Integer.MAX_VALUE;
    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());
        M = Integer.parseInt(st.nextToken());
        student = new PriorityQueue[N];
        for(int i=0;i<N;i++) {
            student[i] = new PriorityQueue<>();
            st = new StringTokenizer(br.readLine());
            for(int j=0;j<M;j++) {
                student[i].offer(Integer.parseInt(st.nextToken()));
            }
        }
        for(int i=0;i<N;i++) {
            int index = i;
            int amount = student[i].poll();
            pq.offer(new Student(index, amount));
            if(amount > max) {
                max = amount;
            }
            if(amount < min) {
                min = amount;
            }
        }
        answer = max - min;
        pick();
        System.out.println(answer);
    }
    public static void pick() {
        // ์ตœ์†Œ๊ฐ’ ๊บผ๋ƒ„
        Student s = pq.poll();
        // ๋‹ค์Œ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ 
        min = pq.peek().amount;
        if(student[s.index].isEmpty()) {
            // ์ตœ์†Œ๊ฐ’์˜ index = N-1
            return;
        }
        // ์ตœ์†Œ๊ฐ’์ด์—ˆ๋˜ index์˜ ๋‹ค์Œ ๊ฐ’ ๋„ฃ์–ด์ค˜์•ผํ•จ
        int p = student[s.index].poll();
        pq.offer(new Student(s.index, p));
        if(p > max) {
            max = p;
        }
        if(p < min) {
            min = p;
        }
        if(answer > max - min) {
            answer = max - min;
        }
        pick();
    }
}

/*
12 16 43 67
7 17 48 68
14 15 54 77

67 68 77

10 20 30
40 50 60
70 80 90
100 110 120

30 40 70 100

์ตœ์†Œ๊ฐ’์ธ๊ฑธ +1, ์ตœ์†Œ๊ฐ’์˜ index = N-1์ด๋ฉด ๋

 */