您的位置:首页 > 产品设计 > UI/UE

POJ 2785 4 Values whose Sum is 0

2017-08-16 12:02 274 查看
you can find the problem here

the question let us to find 4 number from each row which sum fo them is zero. what we should do is to take two rows of tem as one row,get 2∗N∗N number, than us binary search to find compatible solution .

#include<iostream>
#include <cstdio>
#include<cstring>
#include <algorithm>
using namespace std;
const int maxn = 4005;
int a[maxn],b[maxn],c[maxn],d[maxn];
int ab[maxn*maxn],cd[maxn*maxn];
int main()
{
int n = 0;

while(~ scanf("%d",&n))
{
for(int i=1; i<=n; i++) scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
int cnt = 0;
for(int i=1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
ab[cnt] = a[i]+b[j];
cd[cnt++] = c[i]+d[j];
}
}
int ans = 0;
//sort(ab,ab+cnt);
sort(cd,cd+cnt);
for(int i=0; i<cnt; i++)
{
int s = ab[i];
//cout<<ab[i]<<endl;
if(binary_search(cd,cd+cnt,-s))
{
ans+=upper_bound(cd,cd+cnt,-s)-lower_bound(cd,cd+cnt,-s);
}

}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj