https://www.acmicpc.net/problem/7453
- 4개의 배열의 각 인덱스별 합을 구해야함 -> 배열의 최댓값 4000의 4제곱이면 TLE
- 40004000log(4000*4000)으로 시간 충족 가능 -> 이분 탐색
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n;
int a[4001][4];
vector<int> x, y;
void input() {
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 4; ++j) {
cin >> a[i][j];
}
}
}
void solution() {
input();
long long answer = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
x.push_back(a[i][0] + a[j][1]);
y.push_back(a[i][2] + a[j][3]);
}
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
for (int i = 0; i < n * n; ++i) {
answer += upper_bound(y.begin(), y.end(), -x[i])
- lower_bound(y.begin(), y.end(), -x[i]);
}
cout << answer;
}
int main() {
cin.tie(0), cout.tie(0);
ios_base::sync_with_stdio(0);
solution();
return 0;
}
좋은 글 감사합니다.