POJ 2785 4 Values whose Sum is 0

来源:互联网 发布:光猫itv端口改成网口 编辑:程序博客网 时间:2024/05/21 10:14

双向搜索,分组计算ab,与cd,然后二分查找即可。

lower_bound - upper_bound == the number of the items that equal to x.

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[4005], b[4005], c[4005], d[4005];int cd[4005*4005];int main() {int n;while (scanf("%d", &n) != EOF) {for (int i = 0; i < n; i++) {scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cd[i * n + j] = c[i] + d[j];}}sort(cd, cd + n * n);long long res = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {int x = -(a[i] + b[j]);res += upper_bound(cd, cd + n * n, x) - lower_bound(cd, cd + n * n, x);}}printf("%lld\n", res);}}


0 0
原创粉丝点击