3 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅ‡ Gold 5


๐Ÿ“Œ ๋ฌธ์ œ

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

image

image

image


๐Ÿ“Œ ํ’€์ด

์™€โ€ฆ. ์ง„์งœ ์–ด๋ ค์› ๋‹ค.

์ฒ˜์Œ์œผ๋กœ ๋„์ „ํ•˜๋Š” ๊ณจ๋“œ ๋ฌธ์ œ์˜€๋Š”๋ฐ ๋ฌธ์ œ๋‚ด์šฉ์€ ๊ฝค ์žฌ๋ฏธ์žˆ์–ด ๋ณด์˜€๋‹ค.
์ฒ˜์Œ์œผ๋กœ ๋– ์˜ค๋ฅธ ๊ฒƒ์€ 2์ฐจ์› ๋ฐฐ์—ด์— ์ง์ ‘ ๋•…์„ ๊ทธ๋ฆฐ ํ›„ ์ขŒ์šฐ์— ๋•…์ด ์žˆ์œผ๋ฉด ๊ทธ ์‚ฌ์ด ๋นˆ๊ณต๊ฐ„๋งŒํผ finalCount๋ฅผ +1 ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.


์ฒ˜์Œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตณ์ด Show()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ๋•…๋ชจ์–‘์„ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ ค๋‹ค๊ฐ€ ์ค‘๊ฐ„์— ๊ผฌ์—ฌ์„œ 4x4 ๊ฐ™์€ ์ •์‚ฌ๊ฐํ˜• ๋•…์„ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๋•…๋“ค์˜ ๋‹ต์ด ์•ˆ๋‚˜์™”๋‹ค.
์•„๋ฌด๋ฆฌ ์ฐพ์•„๋„ ๋Œ€์ฒด ๋ฌด์Šจ ์˜ค๋ฅ˜์ธ์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋‹ค์‹œ ๊ณ ๋ฏผ์„ ํ•˜์˜€๋‹ค.

    public static void MakeBlocks(int height, int weight) {
        block = new boolean[height][weight];
        for (int i = 0; i < block.length; i++) {
            Arrays.fill(block[i], false);
        }
    }

    public static void FillBlocks(int index, int height) {
        for (int i = 0; i < height; i++) {
            block[index][i] = true;
        }
    }

    public static void Show() {
        for (int i = block[0].length - 1; i > -1; i--) {
            for (int j = 0; j < block.length; j++) {
                System.out.print(block[j][i] + "\t");
            }
            System.out.println("");
        }
    }

    public static int Flow(int index, int width) {
        boolean isBlock = false;
        int FinalCount = 0;
        int Count = 0;
        for (int i = 0; i < width; i++) {
            if (block[i][index]) {
                isBlock = true;
                FinalCount += Count;
            } else {
                if (isBlock) {
                    Count += 1;
                }
            }
        }
        return FinalCount;
    }


๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์˜†์—์„œ ์ž๊ธฐ๋Š” 1์ฐจ์› ๋ฐฐ์—ด์„ ํ†ตํ•ด์„œ ํ’€์—ˆ๋‹ค๊ณ  ํ•˜๊ธธ๋ž˜ ๊ฐ‘์ž๊ธฐ ๋ฒˆ๋œฉ์ด๋Š” ์•„์ด๋””์–ด๊ฐ€ ํ•˜๋‚˜๊ฐ€ ์ƒ๊ฐ์ด ๋‚ฌ๋‹ค.
๊ตณ์ด ๋ชจ๋“  ๋•…์„ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์ด w ๋งŒํผ์˜ 1์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“  ํ›„ 1์ธต๋ถ€ํ„ฐ ๋•…์„ ๋„ฃ์€ํ›„ ์ขŒ์šฐ ๋ฒฝ์ด ์žˆ์œผ๋ฉด ๊ทธ์‚ฌ์ด ๋นˆ๊ณต๊ฐ„ ๋งŒํผ finalCount๋ฅผ +1 ํ•ด์ฃผ๋ฉด ์–ด๋–จ๊นŒ? ํ•˜๋Š” ์ƒ๊ฐ์ด์—ˆ๋‹ค.

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


๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ›์€ h, w๋งŒํผ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์„ false๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์—ˆ๋‹ค.

์ดํ›„ FillBlock()๋ผ๋Š” ํ•จ์ˆ˜์—์„œ ๋ฐ›์€ h๋งŒํผ ๋ฐ‘์—์„œ๋ถ€ํ„ฐ true๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.

๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ฌผ์ด ์–ผ๋งˆํผ ์ฑ„์› ๋Š”์ง€ ์ฒดํฌ๋ฅผ ํ•˜๋Š” ํ•จ์ˆ˜์ธ IsFlow()ํ•จ์ˆ˜์—์„œ boolean type์˜ isBlock๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , int type ์˜ count์™€ finalCount๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.


์ดํ›„ 1์ธต๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ขŒ์ธก์—์„œ ์šฐ์ธก์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด์„œ, ์ง€์ •ํ•œ ์œ„์น˜์˜ block์— true๊ฐ€ ์žˆ์œผ๋ฉด isblock์„ true๋กœ ๋ฐ”๊พธ๊ณ , finalCount์— count๋ฅผ ๋”ํ•˜๊ณ  count๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ๊ทธ ์œ„์น˜์˜ block์ด ๋น„์–ด์žˆ์œผ๋ฉด์„œ isblock์ด false๋ผ๋ฉด count์— +1์„ ํ•ด์ฃผ์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋ฉด fianlCount๋ฅผ returnํ•ด์ฃผ์—ˆ๋‹ค.


์ •๋ง ๋‹คํ–‰์Šค๋Ÿฝ๊ฒŒ๋„ ์ด ์ฝ”๋“œ๋Š” ์•„๋ฌด ๋ฌธ์ œ์—†์ด ํ•œ๋ฒˆ์— ์ž˜ ์ž‘๋™ํ•˜์˜€๋‹ค.
์ฒ˜์Œ์œผ๋กœ ํ‘ผ ๊ณจ๋“œ5์งœ๋ฆฌ ๋ฌธ์ œ์˜€๋Š”๋ฐ, ๋งˆ์น˜ ์–ด๋ ค์šด ํ•œํŽธ์˜ ๊ฒŒ์ž„์„ ํด๋ฆฌ์–ด ํ•œ๊ฑฐ๋งˆ๋ƒฅ ์˜จ๋ชธ์— ์ „์œจ์ด ๋Œ์•˜๋‹ค.
๋‹ค์Œ์—๋Š” FIFO๋‚˜ QUE, STACk๊ฐ™์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•œ ํ›„, ๊ทธ์ชฝ ์ฝ”๋”ฉ ๋ฌธ์ œ๋„ ํ’€์–ด๋ณผ ์ƒ๊ฐ์ด๋‹ค.


์ฝ”๋“œ๋ฅผ ์—ด์‹ฌํžˆ ์งœ๋„ ์ž‘๋™์ด ์•ˆ๋˜์„œ ์ขŒ์ ˆ๊ฐ์ด ๋“ค์—ˆ์ง€๋งŒ, ๊ฒฐ๊ตญ ์„ฑ๊ณตํ•˜๋‹ˆ๊นŒ ํ•œ๋ฐœ์ž๊ตญ ์„ฑ์žฅํ•œ ๋Š๋‚Œ์ด ๋“ค์–ด์„œ ์ข‹์•˜๋‹ค.


๐Ÿ“Œ Code

package Implements;

import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.concurrent.Flow;

public class N14719 {
    public static boolean[][] block;

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

        int height = Integer.parseInt(st.nextToken());
        int width = Integer.parseInt(st.nextToken());

        block = new boolean[height][width];
        for (int i = 0; i < height; i++) {
            Arrays.fill(block[i], false);
        }

        st = new StringTokenizer(br.readLine());
        int result = 0;

        FillBlock(height, width, st);
        for (int i = 0; i < height; i++) {
            result += IsFlow(i, width);
        }
        System.out.println(result);
    }

    public static void FillBlock(int height, int width, StringTokenizer st) {
        int flag = 0;
        for (int w = 0; w < width; w++) {
            int num = Integer.parseInt(st.nextToken());
            for (int h = height - 1; h > height - num - 1; h--) {
                block[h][w] = true;
            }
        }
    }

    public static int IsFlow(int height, int width) {
        boolean isblock = false;
        int count = 0;
        int finalCount=0;
        for (int i = 0; i < width; i++) {
            if (block[height][i]) {
                isblock = true;
                finalCount += count;
                count = 0;
            } else{
                if (isblock) {
                    count += 1;
                }
            }
        }
        return finalCount;
    }
}


package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜