๊ธฐ๋ก๋ฐฉ

BOJ_2628 : ์ข…์ด์ž๋ฅด๊ธฐ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2628 : ์ข…์ด์ž๋ฅด๊ธฐ

Soom_1n 2022. 10. 6. 16:21

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

 

2628๋ฒˆ: ์ข…์ด์ž๋ฅด๊ธฐ

์ฒซ์ค„์—๋Š” ์ข…์ด์˜ ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ ์ฐจ๋ก€๋กœ ์ž์—ฐ์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 100ใŽ์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์นผ๋กœ ์ž˜๋ผ์•ผํ•˜๋Š” ์ ์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ค„๊นŒ์ง€ ํ•œ

www.acmicpc.net



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

  • ์ข…์ด ํฌ๊ธฐ์™€ ์ž๋ฅด๋Š” ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ž˜๋ฆฐ ์ข…์ด ์ค‘ ๊ฐ€์žฅ ํฐ ๋ฉด์ ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • 0, 0๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ขŒํ‘œ๋ฅผ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์ ‘๊ทผํ•˜๋ฏ€๋กœ ๊ฐ€๋กœ ์ปทํŒ…, ์„ธ๋กœ ์ปทํŒ… ์ •๋ณด๊ฐ€ ๋”ฐ๋กœ ์ •๋ ฌ์ด ํ•„์š”ํ•˜๋‹ค.
    • ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ ์ขŒํ‘œ์˜ ํฌ๊ธฐ ์ฐจ์ด๋กœ ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ์ˆ˜์›”ํ•˜๋‹ค.

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
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 r = Integer.parseInt(st.nextToken());
        int c = Integer.parseInt(st.nextToken());
        int n = Integer.parseInt(br.readLine());
        ArrayList<Integer> row = new ArrayList<>();
        ArrayList<Integer> col = new ArrayList<>();

        for (int i = 0; i < n; i++){
            st = new StringTokenizer(br.readLine());
            if (Integer.parseInt(st.nextToken()) == 0)
                row.add(Integer.parseInt(st.nextToken()));
            else
                col.add(Integer.parseInt(st.nextToken()));
        }
        Collections.sort(row);
        Collections.sort(col);

        int answer = 0;
        int x = 0, y = 0;
        for (int i = 0; i <= row.size(); i++){
            int xx = 0, yy;
            for (int j = 0; j <= col.size(); j++){
                if (i == row.size()){
                    xx = c;
                }else xx = row.get(i);
                if (j == col.size()){
                    yy = r;
                }else yy = col.get(j);

                int temp = (xx-x)*(yy-y);
                if (temp > answer) answer = temp;

                if (j != c)
                    y = yy;
            }
            y = 0;
            if (i != r)
                x = xx;
        }
        System.out.println(answer);
    }
}

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

  • ์ข…์ด ํฌ๊ธฐ์™€ n๊ฐœ์˜ ๊ฐ€๋กœ ์„ธ๋กœ ์ปทํŒ… ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
    • ๊ฐ€๋กœ ์„ธ๋กœ์˜ ์ปทํŒ… ์ •๋ณด๋Š” ArrayList์— ๋‹ด๊ณ  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ์ขŒ์ƒ๋‹จ ์ขŒํ‘œ x, y์™€ ์šฐํ•˜๋‹จ ์ขŒํ‘œ xx, yy์˜ ์ฐจ์ด๋กœ ๋„ˆ๋น„๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉฐ, ์ตœ๋Œ€๊ฐ’ answer๋ฅผ ์ €์žฅํ•œ๋‹ค.
    • ์ขŒํ‘œ๋Š” xx๊ฐ€ ๋‹ค์Œ x๊ฐ€ ๋˜๊ณ  yy๊ฐ€ ๋‹ค์Œ y๊ฐ€ ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
    • ๋งˆ์ง€๋ง‰ xx, yy๋Š” ์ข…์ด์˜ ๋ ์ขŒํ‘œ๋ฅผ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ƒ๊ฐ๋ณด๋‹ค ๋ฌธ์ œ ํ’€์ด์˜ ํ‚ค ํฌ์ธํŠธ๋ฅผ ๋ชป์žก์•˜๋Š”๋ฐ, ์ •๋ ฌ์ด ํ•ต์‹ฌ ํ’€์ด ์กฐ๊ฑด์ด์—ˆ๋‹ค.

728x90

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

BOJ_1065 : ํ•œ์ˆ˜  (0) 2022.10.10
BOJ_2635 : ์ˆ˜ ์ด์–ด๊ฐ€๊ธฐ  (0) 2022.10.08
BOJ_2622 : ์‚ผ๊ฐํ˜•๋งŒ๋“ค๊ธฐ  (0) 2022.10.05
BOJ_2338 : ๊ธด์ž๋ฆฌ ๊ณ„์‚ฐ  (0) 2022.10.04
BOJ_1271 : ์—„์ฒญ๋‚œ ๋ถ€์ž2  (0) 2022.10.04