๊ธฐ๋ก๋ฐฉ

BOJ_1954 : ํ™”ํ•™์‹คํ—˜ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1954 : ํ™”ํ•™์‹คํ—˜

Soom_1n 2023. 2. 10. 09:45

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

 

1954๋ฒˆ: ํ™”ํ•™์‹คํ—˜

์šฐ๋ฆฌ์—๊ฒŒ๋Š” n๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ํ™”ํ•™ ์‹œ์•ฝ t1, t2, ..., tn๊ณผ M mg์˜ ์šฉ์•ก์ด ์žˆ๋‹ค. ์ด ์šฉ์•ก ์ค‘ x mg์„ ์‹œ์•ฝ ti์— ๋„ฃ์œผ๋ฉด aix+bi๋งŒํผ์˜ ์–ด๋–ค ๊ฐ€์Šค๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ์‹œ์•ฝ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์šฉ์•ก์˜ ์–‘์€ ์ž์—ฐ์ˆ˜์ด

www.acmicpc.net



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

  • n ์ข…๋ฅ˜์˜ ํ™”ํ•™ ์‹œ์•ฝ t1, t2, ..., tn์˜ ์ •๋ณด ai์™€ bi, ๊ทธ๋ฆฌ๊ณ  M mg์˜ ์šฉ์•ก์ด ์ฃผ์–ด์ง„๋‹ค.
    • ์šฉ์•ก ์ค‘ x mg์„ ์‹œ์•ฝ ti ์— ๋„ฃ์œผ๋ฉด ai*x+bi ๋งŒํผ์˜ ๊ฐ€์Šค๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
    • n๊ฐœ์˜ ํ™”ํ•™ ์‹œ์•ฝ ๋ชจ๋‘ ๊ฐ™์€ ์–‘์˜ ๊ฐ€์Šค๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉด ๊ฐ€์Šค ์–‘์„ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋Ÿด ์ˆ˜ ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์šฉ์•ก์ด ๋‚จ์œผ๋ฉด ์•ˆ๋œ๋‹ค.
  • ์šฉ์•ก์˜ ์–‘ x mg ์„ 1๋ถ€ํ„ฐ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์šฉ์•ก๋“ค์— ๋„ฃ์–ด๋ณด๊ณ  ๊ฐ™์€ ์–‘์˜ ๊ฐ€์Šค๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ๋ชจ๋‘ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.
    • ๊ฒฝ์šฐ์˜ ์ˆ˜ ํ™•์ธ ์ค‘, ๋ชจ๋‘ ๊ฐ™์€ ์–‘์˜ ๊ฐ€์Šค๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ์œผ๋ฉด ๊ทธ ๊ฐ€์Šค ์–‘์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.
    • ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•ด๋„ ๊ฐ™์€ ์–‘์˜ ๊ฐ€์Šค๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

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

public class Main {
    static int n, m;
    static int[] a;
    static int[] b;

    private static boolean recursion(int idx, int ml, int befor_gas) {
        if (idx == n) {
            return ml == 0;
        }

        for (int i = 1; i <= ml-(n-idx-1); i++) {
            int now_gas = a[idx]*i+b[idx];
            if (now_gas == befor_gas && recursion(idx+1, ml - i, befor_gas)) {
                return true;
            } else if (now_gas > befor_gas) {
                break;
            }
        }
        return false;
    }

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());

        a = new int[n];
        b = new int[n];

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

        m = Integer.parseInt(br.readLine());

        int answer = 0;
        for (int i = 1; i <= m - n + 1; i++) {
            int gas = a[0]*i+b[0];
            if (recursion(1, m-i, gas)) {
                answer = gas;
                break;
            }
        }
        System.out.println(answer);
    }
}

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

  • main ๋ฉ”์„œ๋“œ์—์„œ n๊ฐœ์˜ ์‹œ์•ฝ ์ •๋ณด์™€ ์šฉ์•ก์˜ ์–‘ m์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • ์šฉ๋Ÿ‰ m์„ 1๋ถ€ํ„ฐ m-(n-1) ๊นŒ์ง€ ํ‚ค์›Œ๊ฐ€๋ฉฐ ๊ณ„์‚ฐํ•œ๋‹ค.
    • recursion ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , true๊ฐ€ ๋ฐ˜ํ™˜ ๋˜๋ฉด ์ •๋‹ต์— gas๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • recursion ๋ฉ”์„œ๋“œ๋Š” (ํ˜„์žฌ ๊ณ ๋ฅธ ์‹œ์•ฝ ์ˆ˜ idx, ๋‚จ์€ ์šฉ์•ก ml, ์ด์ „์— ๋‚˜์˜จ ๊ฐ€์Šค befor_gas) ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฉด ture,false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • idx๊ฐ€ n์ด ๋˜๋ฉด, ๋‚จ์€ ์šฉ์•ก์ด 0์ด๋ฉด ture, ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (์šฉ์•ก์ด ๋‚จ์œผ๋ฉด ์•ˆ๋œ๋‹ค)
    • for๋ฌธ์—์„œ๋Š” idx๋ฒˆ์งธ ์‹œ์•ฝ์— ๋„ฃ์„ ์šฉ์•ก์„ 1๋ถ€ํ„ฐ ml-(n-idx-1) ๊นŒ์ง€ ๋„ฃ์–ด๋ณธ๋‹ค.
      • ๋‚˜์˜จ ๊ฐ€์Šค now_gas๊ฐ€ ์ด์ „ ๊ฐ€์Šค befor_gas์™€ ๊ฐ™์œผ๋ฉด, ๋‹ค์‹œ recursion ๋ฉ”์„œ๋“œ์— ๋‹ค์Œ ์‹œ์•ฝ์„ ํ™•์ธํ•œ๋‹ค.
      • true๊ฐ€ ๋ฐ˜ํ™˜ ๋˜๋ฉด, ๋˜‘๊ฐ™์ด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      • ๋งŒ์•ฝ now_gas>befor_gas๋ผ๋ฉด, ์‹œ์•ฝ์„ ๋” ๋„ฃ์–ด๋„ ๋” ๋งŽ์€ ๊ฐ€์Šค๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋ฏ€๋กœ now_gas==befor-gass์ธ ์ƒํ™ฉ์ด ์˜ค์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ break๋กœ ๋น ์ ธ๋‚˜์˜จ๋‹ค.
      • false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์šฉ์•ก์˜ ์–‘์„ ์–ด๋–ป๊ฒŒ ์กฐ์ ˆํ•ด์•ผ ํ•˜๋‚˜ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€, ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๋”ฐ์ ธ์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์žฌ๊ท€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์žฌ๊ท€ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด for๋ฌธ์˜ ์ข…๋ฃŒ ์กฐ๊ฑด์„ ์ง€์ •ํ•˜๋Š”๊ฒŒ ์–ด๋ ค์› ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
  • ๋ฌธ์ œ ๋ถ„์„๊ณผ ํ’€์ด ๊ณ„ํš์„ ์ ์–ด๊ฐ€๋ฉฐ ํ’€์ดํ–ˆ๋‹ค.

728x90