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 32a3ed5

Browse files
committed
손지민: [BOJ] 냅색문제250222
1 parent 7be8f65 commit 32a3ed5

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.StringTokenizer;
8+
9+
/**
10+
* 완전 틀림, 해결책 제시 X
11+
*/
12+
public class JM_1450 {
13+
static int N;
14+
static int C;
15+
static int[] W;
16+
17+
private static int upperBound(int target, List<Integer> A) {
18+
int lo = 0;
19+
int hi = A.size() - 1;
20+
int ans = A.size();
21+
while (lo <= hi) {
22+
int mid = (lo + hi) / 2;
23+
if(target < A.get(mid)) {
24+
ans = mid;
25+
hi = mid - 1;
26+
}
27+
else lo = mid + 1;
28+
}
29+
return ans;
30+
}
31+
32+
private static void combi(int start, int end, int sum, List<Integer> combiSums) {
33+
if(sum > C) return;
34+
if(start == end) {
35+
combiSums.add(sum);
36+
return;
37+
}
38+
combi(start + 1, end, sum, combiSums);
39+
combi(start + 1, end, sum + W[start], combiSums);
40+
}
41+
42+
private static int solve() {
43+
List<Integer> left = new ArrayList<>();
44+
List<Integer> right = new ArrayList<>();
45+
46+
combi(0, N / 2, 0, left);
47+
combi(N / 2, N, 0, right);
48+
49+
Collections.sort(right);
50+
51+
int count = 0;
52+
for(int leftSum : left) {
53+
count += upperBound(C - leftSum, right);
54+
}
55+
return count;
56+
}
57+
58+
public static void main(String[] args) throws IOException {
59+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
60+
StringTokenizer st = new StringTokenizer(br.readLine());
61+
N = Integer.parseInt(st.nextToken());
62+
C = Integer.parseInt(st.nextToken());
63+
W = new int[N];
64+
65+
st = new StringTokenizer(br.readLine());
66+
for (int i = 0; i < N; i++) {
67+
W[i] = Integer.parseInt(st.nextToken());
68+
}
69+
70+
System.out.println(solve());
71+
}
72+
}

0 commit comments

Comments
(0)

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