Home 20181(꿈틀꿈틀 호석 애벌래)
Post
Cancel

20181(꿈틀꿈틀 호석 애벌래)

첫 시도

  • K는 100이지만 시간 복잡도에 별다른 영향이 없음
  • N은 20이므로 O(N^7)정도까지는 여유롭게 가능
  • dfs 완전탐색으로 시도

해결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    static int[] arr;
    static int N,K;
    static int max;

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        max = 0;
        arr = new int[N];

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

        dfs(0,0,0);
        System.out.println(max);
    }

    static void dfs(int index, int sum, int total){
        if(index == arr.length) {
            max = Math.max(max, sum);
            return;
        }

            total += arr[index];
            if(total>=K){
                dfs(index+1, sum + total - K, 0);
            }
            else{
                dfs(index+1,sum, total);
            }
            dfs(index+1,sum,0);

    }
}

참고

This post is licensed under CC BY 4.0 by the author.