๊ธฐ๋ก๋ฐฉ

BOJ_13305 : ์ฃผ์œ ์†Œ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_13305 : ์ฃผ์œ ์†Œ

Soom_1n 2022. 12. 29. 00:43

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

 

13305๋ฒˆ: ์ฃผ์œ ์†Œ

ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์Œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N(2 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” ์ธ์ ‘ํ•œ ๋‘ ๋„์‹œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ธธ์ด๊ฐ€ ์ œ์ผ ์™ผ์ชฝ ๋„๋กœ๋ถ€ํ„ฐ N-1

www.acmicpc.net



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

  • n๊ฐœ์˜ ๋งˆ์„์ด ์ผ๋ ฌ๋กœ ์ฃผ์–ด์ง€๋ฉฐ n-1๊ฐœ์˜ ๋„๋กœ๊ฐ€ ์žˆ๋‹ค.
    • ๋งˆ์„๋งˆ๋‹ค 1L๋‹น ๊ธฐ๋ฆ„๊ฐ’์ด ์ฃผ์–ด์ง€๋ฉฐ, ๋„๋กœ 1km๋‹น ๊ธฐ๋ฆ„ 1L๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
    • ์ตœ์ข… ๊ธฐ๋ฆ„๊ฐ’์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ „ํ˜•์ ์ธ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ด๋‹ค.
    • ๊ฐ€์žฅ ๊ฐ€์„ฑ๋น„ ์ข‹๊ฒŒ ๊ธฐ๋ฆ„์„ ๊ตฌ์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์ตœ์ข… ๋„์‹œ์—์„œ ๊ฑฐ๊พธ๋กœ ๋นผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.
  • ๋„๋กœ์˜ ๊ธธ์ด์™€ ๋„์‹œ ๋ณ„ ๊ธฐ๋ฆ„๊ฐ’์˜ ์ตœ๋Œ€ ๊ฐ’์ด 10์–ต์ด๋ฏ€๋กœ longํ˜•์„ ์‚ฌ์šฉํ•œ๋‹ค.

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

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long[] road = new long[n - 1];
        long[] city = new long[n];
        int min_index = 0;
        long min = 1000000001;
        long r = 0;
        long answer = 0;

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n - 1; i++) {
            road[i] = Integer.parseInt(st.nextToken());
            r += road[i];
        }

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            city[i] = Integer.parseInt(st.nextToken());
            if (min > city[i] && i < n - 1) {
                min = city[i];
                min_index = i;
            }
        }

        n--;

        while (r > 0) {
            long len = 0;
            for (int i = n-1; i >= min_index; i--) {
                len += road[i];
            }

            answer += min * len;
            r -= len;

            n = min_index;
            min = 1000000001;
            for (int i = 0; i < n; i++) {
                if (city[i] < min) {
                    min_index = i;
                    min = city[i];
                }
            }
        }
        System.out.println(answer);
    }
}

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

  • n-1๊ฐœ์˜ ๋„๋กœ ๊ธธ์ด์™€ n๊ฐœ์˜ ๋„์‹œ ๋ณ„ ๊ธฐ๋ฆ„๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • ๋‚จ์€ ๋„๋กœ ๊ธธ์ด r์ด 0์ด ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • ํ˜„์žฌ ์œ„์น˜๋ถ€ํ„ฐ ๊ธฐ๋ฆ„๊ฐ’์ด ์ตœ์†Œ์ธ ๋„์‹œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ r์—์„œ ๋บ€๋‹ค.
    • ๊ตฌํ•œ ๊ฑฐ๋ฆฌ x ๊ธฐ๋ฆ„ ๊ฐ’์„ ์ถœ๋ ฅ ๋ณ€์ˆ˜ answer์— ๋ˆ„์ ํ•œ๋‹ค.
    • ํ˜„์žฌ ์œ„์น˜๋ฅผ ์ด๋™ํ•˜๊ณ , ๊ธฐ๋ฆ„๊ฐ’์ด ์ตœ์†Œ์ธ ๋„์‹œ๋ฅผ ์ฐพ๋Š”๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์˜ค๋žœ๋งŒ์— ์ฝ”ํ…Œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์„œ ๊ทธ๋Ÿฐ์ง€ ๊ฐ์ด ์•ˆ์žกํ˜€์„œ ํƒœ๊ทธ๋ฅผ ๋ณด๊ณ  ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํžŒํŠธ๋ฅผ ์–ป์–ด ํ’€์—ˆ๋‹ค.

728x90

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

BOJ_1205 : ๋“ฑ์ˆ˜ ๊ตฌํ•˜๊ธฐ  (0) 2022.12.30
BOJ_11656 : ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด  (0) 2022.12.30
BOJ_1904 : 01ํƒ€์ผ  (0) 2022.12.18
BOJ_14501 : ํ‡ด์‚ฌ  (0) 2022.12.17
BOJ_15657 : N๊ณผ M (8)  (0) 2022.12.15