UVA

题目:点击打开题目链接

思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn)

AC代码:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int maxn = 4005;
 6 
 7 
 8 int main()
 9 {
10     ios::sync_with_stdio(false);
11     cin.tie(0);
12     int T, n, ans; cin >> T;
13     int A[maxn], B[maxn], C[maxn], D[maxn];
14     while(T--) {
15         ans = 0;
16 
17         vector<int> vec;
18         cin >> n;
19         for(int i = 0; i < n; ++i) {
20             cin >> A[i] >> B[i] >> C[i] >> D[i];
21         }
22 
23         for(int i = 0; i < n; i++)
24             for(int j = 0; j < n; j++)
25                 vec.push_back(A[i] + B[j]);
26         sort(vec.begin(), vec.end());
27 
28         for(int i = 0; i < n; i++)
29             for(int j = 0; j < n; j++)
30                 ans += upper_bound(vec.begin(), vec.end(), -(C[i] + D[j])) - lower_bound(vec.begin(), vec.end(),-(C[i] + D[j]));
31 
32         cout << ans << endl;
33         if(T) cout << endl;
34     }
35     return 0;
36 }
版权声明:该博客版权归本人所有,若有意转载,请与本人联系
原文地址:https://www.cnblogs.com/fan-jiaming/p/9440205.html