๊ธฐ๋ก๋ฐฉ

BOJ_2535 : ์•„์‹œ์•„ ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2535 : ์•„์‹œ์•„ ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ

Soom_1n 2022. 11. 9. 17:43

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

 

2535๋ฒˆ: ์•„์‹œ์•„ ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋Œ€ํšŒ์ฐธ๊ฐ€ ํ•™์ƒ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹จ, 3 ≤ N ≤ 100์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ํ•œ ํ•™์ƒ์˜ ์†Œ์† ๊ตญ๊ฐ€ ๋ฒˆํ˜ธ, ํ•™์ƒ ๋ฒˆํ˜ธ, ๊ทธ๋ฆฌ๊ณ  ์„ฑ์ ์ด ํ•˜๋‚˜์˜ ๋นˆ์นธ์„ ์‚ฌ

www.acmicpc.net



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

  • ๋Œ€ํšŒ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ ์ˆ˜ ์ˆœ์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • 1, 2, 3๋“ฑ ์ˆœ์œผ๋กœ ๊ธˆ๋ฉ”๋‹ฌ, ์€๋ฉ”๋‹ฌ, ๋™๋ฉ”๋‹ฌ์„ ์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•œ ๊ตญ๊ฐ€์—์„œ ๋ฉ”๋‹ฌ์„ ์ตœ๋Œ€ 2๊ฐœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

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

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));
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());
        int[] counrtry = new int[n+1];
        Mydata[] arr = new Mydata[n];

        for (int i = 0; i < n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            arr[i] = new Mydata(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
        }
        Arrays.sort(arr);

        int count = 0, i = 0;
        while (count < 3){
            if (counrtry[arr[i].country] < 2) {
                counrtry[arr[i].country]++;
                sb.append(arr[i].country + " " + arr[i].student + "\n");
                count++;
            }
            i++;
        }
        System.out.println(sb.toString());
    }
}

class Mydata implements Comparable<Mydata> {
    int country;
    int student;
    int grade;

    public Mydata(int country, int student, int grade) {
        this.country = country;
        this.student = student;
        this.grade = grade;
    }

    @Override
    public int compareTo(Mydata o) {
        return o.grade - this.grade;
    }
}

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

  • ๊ตญ๊ฐ€ ๋ฒˆํ˜ธ, ํ•™์ƒ ๋ฒˆํ˜ธ, ๋Œ€ํšŒ ์„ฑ์ ์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์‚ฌ์šฉ์ž ์ง€์ • ์ •๋ ฌ๋กœ ๋Œ€ํšŒ ์„ฑ์ ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  • 3๋ฒˆ์˜ ๋ฉ”๋‹ฌ์ด ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • ํ•œ ๊ตญ๊ฐ€๊ฐ€ ๋ฐ›์€ ๋ฉ”๋‹ฌ์„ ์นด์šดํŠธํ•ด์„œ 2๋ฒˆ์ด์ƒ ๋ฐ›์€ ๊ตญ๊ฐ€๋Š” ๋„˜์–ด๊ฐ„๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์‚ฌ์šฉ์ž ์ง€์ • ๋ฐฐ์—ด์„ ๊ณต๋ถ€ํ•œ ๋’ค ๋น„์Šทํ•œ ์–‘์ƒ์œผ๋กœ ๋‚˜์˜จ ๋ฌธ์ œ์—ฌ์„œ ์ €๋ฒˆ์— ์“ด ๋ฐฉ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด ๋ณด์•˜๋‹ค.
  • ์™ธ๋ถ€์— ์ƒˆ๋กœ์šด ํด๋ž˜์Šค Mydata๋ฅผ ์„ ์–ธํ•ด์„œ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
    • ์ƒˆ๋กœ์šด ์ •๋ ฌ ์กฐ๊ฑด์œผ๋กœ ๋Œ€ํšŒ ์„ฑ์ ์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ ๋ณด๋‹ค. Arrays.sort()์—์„œ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ new Comparable<Integer[ ]>๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋” ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1431 : ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ  (0) 2022.11.12
BOJ_10825 : ๊ตญ์˜์ˆ˜  (0) 2022.11.10
BOJ_1377 : ๋ฒ„๋ธ” ์†ŒํŠธ  (0) 2022.11.08
BOJ_2750 : ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ  (0) 2022.11.08
BOJ_11286 : ์ ˆ๋Œ“๊ฐ’ ํž™  (0) 2022.11.07