Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6bc30ba

Browse files
committed
백제완: [BOJ] 2629 양팔저울_250228
1 parent 367e982 commit 6bc30ba

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎BOJ/1000-5000번/JW_2629.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class JW_2629 {
2+
3+
public static void main(String[] args) throws Exception {
4+
int n = read();
5+
int[] weights = new int[n + 1];
6+
for (int i = 1; i < n + 1; i++)
7+
weights[i] = read();
8+
int m = read(), maxTarget = 0;
9+
int[] targets = new int[m];
10+
for (int i = 0; i < m; i++) {
11+
targets[i] = read();
12+
maxTarget = Math.max(maxTarget, targets[i]);
13+
}
14+
// 가능한 범위까지 DP 배열 생성
15+
boolean[] dp = new boolean[maxTarget + 501];
16+
dp[0] = true;
17+
int w;
18+
for (int i = 1; i < n + 1; i++) {
19+
w = weights[i];
20+
boolean[] temp = dp.clone(); // 영향을 주지 않기 위해 새로운 배열 생성
21+
for (int j = dp.length - 1; j >= 0; j--) {
22+
if (dp[j]) {
23+
if (j + w < dp.length)
24+
temp[j + w] = true; // 더하기
25+
if (j - w >= 0)
26+
temp[j - w] = true; // 뺴기
27+
if (w - j >= 0)
28+
temp[w - j] = true; // 반대편에 놓기
29+
}
30+
}
31+
dp = temp;
32+
}
33+
StringBuilder sb = new StringBuilder();
34+
for (int i = 0; i < m; i++)
35+
sb.append(dp[targets[i]] ? 'Y' : 'N').append(' ');
36+
System.out.println(sb);
37+
}
38+
39+
private static int read() throws Exception {
40+
int c, n = System.in.read() & 15;
41+
while ((c = System.in.read()) >= 48)
42+
n = (n << 3) + (n << 1) + (c & 15);
43+
if (c == 13)
44+
System.in.read();
45+
return n;
46+
}
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /