-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBJ7453.java
More file actions
70 lines (65 loc) · 2.01 KB
/
BJ7453.java
File metadata and controls
70 lines (65 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BJ7453 {
/* 7453. 합이 0인 네 정수
정렬
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[] A = new int[N];
int[] B = new int[N];
int[] C = new int[N];
int[] D = new int[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
A[i] = Integer.parseInt(st.nextToken());
B[i] = Integer.parseInt(st.nextToken());
C[i] = Integer.parseInt(st.nextToken());
D[i] = Integer.parseInt(st.nextToken());
}
int[] AB = new int[N*N];
int[] CD = new int[N*N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
AB[N*i+j] = A[i] + B[j];
CD[N*i+j] = -(C[i] + D[j]);
}
}
Arrays.sort(AB);
Arrays.sort(CD);
long result = 0;
int i = 0;
int j = 0;
long lCnt = 0;
long rCnt = 0;
while (i < N*N-1 || j < N*N-1){
if (AB[i] == CD[j]) {
lCnt = 1;
rCnt = 1;
while (i+1 < N*N && AB[i+1] == AB[i]) {
lCnt++;
i++;
}
while (j+1 < N*N && CD[j+1] == CD[j]) {
rCnt++;
j++;
}
result += (lCnt * rCnt);
if (i+1 < N*N) i++;
if (j+1 < N*N) j++;
continue;
}
if (i == N*N-1) j++;
else if (j == N*N-1) i++;
else if (AB[i] > CD[j]) j++;
else if (AB[i] < CD[j]) i++;
}
System.out.println(result);
}
}