๊ธฐ๋ก๋ฐฉ

BOJ_8979 : ์˜ฌ๋ฆผํ”ฝ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_8979 : ์˜ฌ๋ฆผํ”ฝ

Soom_1n 2022. 11. 20. 21:28

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

8979๋ฒˆ: ์˜ฌ๋ฆผํ”ฝ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์€ ๊ตญ๊ฐ€์˜ ์ˆ˜ N(1 ≤ N ≤ 1,000)๊ณผ ๋“ฑ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ๊ตญ๊ฐ€ K(1 ≤ K ≤ N)๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๊ตญ๊ฐ€๋Š” 1๋ถ€ํ„ฐ N ์‚ฌ์ด์˜ ์ •์ˆ˜๋กœ ํ‘œํ˜„๋œ๋‹ค. ์ดํ›„ N๊ฐœ์˜ ๊ฐ ์ค„์—๋Š” ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • n๊ฐœ ๊ตญ๊ฐ€์˜ ์˜ฌ๋ฆผํ”ฝ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, m๋ฒˆ์งธ ๊ตญ๊ฐ€๊ฐ€ ๋ช‡ ๋“ฑ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        MyData_8979[] data = new MyData_8979[n];
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            data[i] = new MyData_8979(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
        }

        Arrays.sort(data);

        int answer = 1;
        for (int i = 0; i < n; i++) {
            if (data[i].n == k || i == n-1) {
                System.out.println(answer);
                break;
            }
            if (data[i].g != data[i+1].g || data[i].s != data[i+1].s || data[i].b != data[i+1].b)
                answer = i+2;
        }
    }
}

class MyData_8979 implements Comparable<MyData_8979> {
    int n, g, s, b;
    public MyData_8979(int n, int g, int s, int b) {
        this.n = n;
        this.g = g;
        this.s = s;
        this.b = b;
    }

    @Override
    public int compareTo(MyData_8979 o) {
        if (this.g != o.g)
            return o.g - this.g;
        else if (this.s != o.s)
            return o.s - this.s;
        else
            return o.b - this.b;
    }
}

 

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉ์ž์ง€์ • ์ •๋ ฌ๋กœ ๊ตญ๊ฐ€ ๋ณ„ ์˜ฌ๋ฆผํ”ฝ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋‹ฌ ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ๋‹ค.
  • k๋ฒˆ์žฌ ๊ตญ๊ฐ€์˜ ์ˆœ์œ„๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์ •๋ ฌ๋œ ์˜ฌ๋ฆผํ”ฝ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์—์„œ ๋ฉ”๋‹ฌ ์ˆ˜๊ฐ€ ๋ชจ๋‘ ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ์ˆœ์œ„๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์‚ฌ์šฉ์ž ์ง€์ • ์ •๋ ฌ์€ ์ด์ œ ์ถฉ๋ถ„ํžˆ ์—ฐ์Šต๋˜์–ด์„œ ์–ด๋ ค์›€์ด ์—†์—ˆ์ง€๋งŒ, ์ˆœ์œ„๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๊ฒŒ ์˜คํžˆ๋ ค ๋” ์–ด๋ ค์› ๋‹ค.
  • ๊ตญ๊ฐ€์˜ ์ˆ˜ n์ด 2์ผ ๋•Œ ์ธ๋ฑ์Šค ์˜ค๋ฅ˜๊ฐ€ ์žˆ์–ด์„œ 1ํšŒ ํ‹€๋ ธ๋‹ค.

728x90