-
Notifications
You must be signed in to change notification settings - Fork 0
/
7453_합이0인네정수.cpp
78 lines (61 loc) · 1.29 KB
/
7453_합이0인네정수.cpp
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
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
bool comp(const int& a, const int& b) {
return a > b;
}
int main() {
int n;
scanf("%d", &n);
vector<int> A, B, C, D;
for (int i = 0; i < n; i++) {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
A.push_back(a);
B.push_back(b);
C.push_back(c);
D.push_back(d);
}
vector<int> AB, CD;
// AB (A + B)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
AB.push_back(A[i] + B[j]);
}
}
// CD (C + D)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
CD.push_back(C[i] + D[j]);
}
}
// sorting
sort(AB.begin(), AB.end());
sort(CD.begin(), CD.end(), comp);
// two pointer
int ptab = 0, ptcd = 0;
long long int result = 0;
while (ptab < AB.size() && ptcd < CD.size()) {
int currentAB = AB[ptab];
int target = -currentAB;
if (CD[ptcd] == target) {
int ab = 0, cd = 0;
while(AB[ptab] == currentAB && ptab < AB.size()){
ab++;
ptab++;
}
while(CD[ptcd] == target && ptcd < CD.size()){
cd++;
ptcd++;
}
result += (long long int) ab * cd;
}
else if (CD[ptcd] > target) {
ptcd++;
}
else {
ptab++;
}
}
printf("%lld\n", result);
return 0;
}