求出前两列的两两一对的和存到数组里,然后枚举后面两行两两一对的和并求他们的相反数,然后在对数组进行排序,使用二分查找后面两行两两一对的和的相反数。
#include <iostream> #include <vector> #include <algorithm> using namespace std; int n; const int maxn = 4010; int arr[maxn][4]; vector<int>temp; int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { cin >> arr[i][j]; } } int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp.push_back(arr[i][0] + arr[j][1]); } } sort(temp.begin(), temp.end()); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int num = -(arr[i][2] + arr[j][3]); ans+= upper_bound(temp.begin(), temp.end(), num) - lower_bound(temp.begin(), temp.end(), num); } } cout << ans; }