标签:2785 int Sum 30 sum2 sum1 pos ++ Values
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
输入描述:
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2
28 ) that belong respectively to A, B, C and D .
输出描述:
For each input file, your program has to write the number quadruplets whose sum is zero.
示例1
输入
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
输出
5
备注:
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 4010;
int a[N], b[N], c[N], d[N], sum1[N * N + 10], sum2[N * N + 10], n;
int main()
{
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
int k = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
sum1[k] = a[i] + b[j];
sum2[k] = c[i] + d[j];
k++;
}
int ans = 0;
sort(sum1, sum1 + k), sort(sum2, sum2 + k);
for(int i = 0; i < k; i++)
{
int pos = lower_bound(sum2, sum2 + k, -sum1[i]) - sum2;
while(sum2[pos] == -sum1[i] && pos < k)
ans++, pos++;
}
cout << ans << endl;
return 0;
}
标签:2785,int,Sum,30,sum2,sum1,pos,++,Values 来源: https://blog.csdn.net/qq_44826711/article/details/114017252
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。