Skip to content

Commit 9510b20

Browse files
authored
Create BJ_1943_동전분배.java
1 parent 111e731 commit 9510b20

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

250414/BJ_1943_동전분배.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.StringTokenizer;
7+
8+
/**
9+
* 백준 1943번 동전 분배
10+
* - 배낭
11+
*/
12+
13+
public class Main {
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st;
17+
18+
for (int t = 0; t < 3; t++) {
19+
int N = Integer.parseInt(br.readLine());
20+
21+
int sum = 0;
22+
23+
int[][] coins = new int[N][2];
24+
25+
for (int i = 0; i < N; i++) {
26+
st = new StringTokenizer(br.readLine());
27+
int amount = Integer.parseInt(st.nextToken());
28+
int count = Integer.parseInt(st.nextToken());
29+
30+
coins[i][0] = amount;
31+
coins[i][1] = count;
32+
33+
sum += amount * count;
34+
}
35+
36+
if (sum % 2 == 1) {
37+
System.out.println(0);
38+
continue;
39+
}
40+
41+
int mid = sum / 2;
42+
43+
boolean[] dp = new boolean[mid + 1];
44+
boolean[] tmp = new boolean[mid + 1];
45+
dp[0] = true;
46+
47+
for (int i = 0; i < N; i++) {
48+
int amount = coins[i][0];
49+
int count = coins[i][1];
50+
51+
tmp = dp.clone();
52+
53+
for (int j = 1; j <= count; j++) {
54+
int cost = amount * j;
55+
for (int k = 0; k + cost <= mid; k++) {
56+
if (dp[k]) tmp[k + cost] = true;
57+
}
58+
}
59+
60+
dp = tmp;
61+
}
62+
63+
System.out.println(dp[mid] ? 1 : 0);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)