Skip to content

Commit 5e718e2

Browse files
committed
손지민 [BOJ] 합이 0_250320
1 parent ec05624 commit 5e718e2

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

BOJ/1000-5000번/JM_3151.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class JM_3151 {
8+
static int N;
9+
static int[] A;
10+
11+
private static int lowerBound(int startIndex, int target) {
12+
int lo = startIndex, hi = N - 1;
13+
int ans = N;
14+
while (lo <= hi){
15+
int mid = (lo + hi) / 2;
16+
if(target <= A[mid]) {
17+
ans = mid;
18+
hi = mid - 1;
19+
}
20+
else lo = mid + 1;
21+
}
22+
return ans;
23+
}
24+
25+
private static int upperBound(int startIndex, int target) {
26+
int lo = startIndex, hi = N - 1;
27+
int ans = N;
28+
while (lo <= hi){
29+
int mid = (lo + hi) / 2;
30+
if(target < A[mid]) {
31+
ans = mid;
32+
hi = mid - 1;
33+
}
34+
else lo = mid + 1;
35+
}
36+
return ans;
37+
}
38+
39+
private static long solve() {
40+
Arrays.sort(A);
41+
long count = 0;
42+
for (int i = 0; i < N; i++) {
43+
for (int j = i + 1; j < N; j++) {
44+
int target = 0 - (A[i] + A[j]);
45+
int lowerIndex = lowerBound(j + 1, target);
46+
int upperIndex = upperBound(j + 1, target);
47+
count += upperIndex - lowerIndex;
48+
}
49+
}
50+
return count;
51+
}
52+
53+
public static void main(String[] args) throws IOException {
54+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
55+
StringTokenizer st = new StringTokenizer(br.readLine());
56+
N = Integer.parseInt(st.nextToken());
57+
58+
A = new int[N];
59+
st = new StringTokenizer(br.readLine());
60+
for(int i = 0; i < N; i++) {
61+
A[i] = Integer.parseInt(st.nextToken());
62+
}
63+
System.out.println(solve());
64+
}
65+
}

0 commit comments

Comments
 (0)