|
| 1 | +// Authored by: beberiche |
| 2 | +// Co-authored by: - |
| 3 | +// Link: http://boj.kr/7aa1cbf4fecc4f928327f2786fa1da53 |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.io.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + public static void main(String[] args) { |
| 10 | + FastReader rd = new FastReader(); |
| 11 | + int N = rd.nextInt(); |
| 12 | + |
| 13 | + List<Integer> list = new ArrayList<>(); |
| 14 | + |
| 15 | + // 육각수 만들기 |
| 16 | + list.add(1); |
| 17 | + int sum = 5; |
| 18 | + while (list.get(list.size() - 1) + sum <= N) { |
| 19 | + list.add(list.get(list.size() - 1) + sum); |
| 20 | + sum += 4; |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + int dp[] = new int[N + 1]; |
| 25 | + for (int i = 1; i <= N; i++) { |
| 26 | + dp[i] = 6; |
| 27 | + for (int s : list) { |
| 28 | + if (s > i) break; |
| 29 | + dp[i] = Math.min(dp[i], dp[i - s] + 1); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + System.out.println(dp[N]); |
| 34 | + } |
| 35 | + |
| 36 | + static class FastReader { |
| 37 | + BufferedReader br; |
| 38 | + StringTokenizer st; |
| 39 | + |
| 40 | + public FastReader() { |
| 41 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 42 | + } |
| 43 | + |
| 44 | + String next() { |
| 45 | + while (st == null || !st.hasMoreElements()) { |
| 46 | + try { |
| 47 | + st = new StringTokenizer(br.readLine()); |
| 48 | + } catch (IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + } |
| 52 | + return st.nextToken(); |
| 53 | + } |
| 54 | + |
| 55 | + int nextInt() { |
| 56 | + return Integer.parseInt(next()); |
| 57 | + } |
| 58 | + |
| 59 | + long nextLong() { |
| 60 | + return Long.parseLong(next()); |
| 61 | + } |
| 62 | + |
| 63 | + double nextDouble() { |
| 64 | + return Double.parseDouble(next()); |
| 65 | + } |
| 66 | + |
| 67 | + String nextLine() { |
| 68 | + String str = ""; |
| 69 | + try { |
| 70 | + str = br.readLine(); |
| 71 | + } catch (IOException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + return str; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/* Solution Description |
| 80 | +
|
| 81 | +1. 냅색 응용 문제. `N` 만큼의 육각수를 만든 후, |
| 82 | + 이를 기반으로 냅색을 통해 문제를 해결할 수 있다. |
| 83 | +
|
| 84 | +2. 육각수는 결국, 육각형 중 2개의 선분을 모형이다. |
| 85 | + 즉, 임의의 육각수의 크기를 $h_n$ 이라 가정한다면, |
| 86 | + 현재 육각수의 크기는 $h_{n-1}$ + 나머지 4개 선분의 점의 갯수가 된다. |
| 87 | +
|
| 88 | +3. $n$ 이 커질수록 선분 별로 1개씩 점이 추가되므로, |
| 89 | + $h_1$ 의 4개 선분의 점의 갯수인 `5` 를 시작으로 4씩 추가해가며 육각수의 크기를 구할 수 있다. |
| 90 | +
|
| 91 | +4. 이후 냅색을 통해 `dp[N]` 의 최솟값을 구하면 된다. |
| 92 | +
|
| 93 | + */ |
0 commit comments