Skip to content

Commit 7c5474b

Browse files
authored
Merge pull request #360 from AlgorithmWithGod/lkhyun
[20250616] BOJ / G2 / 합이 0인 네 정수 / 이강현
2 parents 85e3519 + de707b9 commit 7c5474b

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N;
10+
static int[] A;
11+
static int[] B;
12+
static int[] C;
13+
static int[] D;
14+
static int[] AB;
15+
static int[] CD;
16+
static long answer = 0;
17+
18+
public static void main(String[] args) throws IOException {
19+
N = Integer.parseInt(br.readLine());
20+
A = new int[N];
21+
B = new int[N];
22+
C = new int[N];
23+
D = new int[N];
24+
for (int i = 0; i < N; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
A[i] = Integer.parseInt(st.nextToken());
27+
B[i] = Integer.parseInt(st.nextToken());
28+
C[i] = Integer.parseInt(st.nextToken());
29+
D[i] = Integer.parseInt(st.nextToken());
30+
}
31+
32+
AB = new int[N*N];
33+
CD = new int[N*N];
34+
for (int i = 0; i < N; i++) {
35+
for (int j = 0; j < N; j++) {
36+
AB[i*N+j] = A[i] + B[j];
37+
CD[i*N+j] = C[i] + D[j];
38+
}
39+
}
40+
Arrays.sort(AB);
41+
Arrays.sort(CD);
42+
43+
int left = 0, right = N*N-1;
44+
while (left < N*N && right >= 0) {
45+
if(AB[left] + CD[right] == 0){
46+
int leftCnt = 1;
47+
while(left + 1 < N*N && AB[left+1] == AB[left]){
48+
left++;
49+
leftCnt++;
50+
}
51+
int rightCnt = 1;
52+
while(right - 1 >= 0 && CD[right-1] == CD[right]){
53+
right--;
54+
rightCnt++;
55+
}
56+
answer += (long)leftCnt * rightCnt;
57+
left++;
58+
right--;
59+
}else if(AB[left] + CD[right] < 0){
60+
left++;
61+
}else{
62+
right--;
63+
}
64+
}
65+
bw.write(answer + "\n");
66+
bw.close();
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)