Skip to content

Commit 1f68240

Browse files
authored
[20260129] BOJ / G2 / 합이 0인 네 정수 / 김민진
1 parent d669569 commit 1f68240

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Arrays;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_7453_합이_0인_네_정수 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static StringTokenizer st;
11+
12+
private static int N;
13+
private static long cnt;
14+
private static int[] A, B, C, D;
15+
private static int[] AB, CD;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
sol();
20+
}
21+
22+
private static void init() throws IOException {
23+
N = Integer.parseInt(br.readLine());
24+
cnt = 0;
25+
26+
A = new int[N];
27+
B = new int[N];
28+
C = new int[N];
29+
D = new int[N];
30+
for (int i = 0; i < N; i++) {
31+
st = new StringTokenizer(br.readLine());
32+
A[i] = Integer.parseInt(st.nextToken());
33+
B[i] = Integer.parseInt(st.nextToken());
34+
C[i] = Integer.parseInt(st.nextToken());
35+
D[i] = Integer.parseInt(st.nextToken());
36+
}
37+
38+
AB = new int[N * N];
39+
CD = new int[N * N];
40+
}
41+
42+
private static void sol() throws IOException {
43+
int idx = 0;
44+
for (int i = 0; i < N; i++) {
45+
for (int j = 0; j < N; j++) {
46+
AB[idx] = A[i] + B[j];
47+
CD[idx++] = C[i] + D[j];
48+
}
49+
}
50+
Arrays.sort(AB);
51+
Arrays.sort(CD);
52+
53+
int left = 0;
54+
int right = N * N - 1;
55+
while (left < N * N && right >= 0) {
56+
int sum = AB[left] + CD[right];
57+
58+
if (sum == 0) {
59+
int lVal = AB[left];
60+
int rVal = CD[right];
61+
62+
long lCnt = 1;
63+
long rCnt = 1;
64+
while (++left < N * N && AB[left] == lVal) {
65+
lCnt++;
66+
}
67+
while (--right >= 0 && CD[right] == rVal) {
68+
rCnt++;
69+
}
70+
cnt += (long) lCnt * rCnt;
71+
} else if (sum < 0) {
72+
left++;
73+
} else {
74+
right--;
75+
}
76+
}
77+
bw.write(cnt + "");
78+
bw.flush();
79+
bw.close();
80+
br.close();
81+
}
82+
83+
}
84+
```

0 commit comments

Comments
 (0)