思路:

如果用朴素的方法算O(n^4)超时,这里用折半二分。把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数。

注意和有重复

#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int N = 4000+5;
int a[N],b[N],c[N],d[N];
int mp1[N*N],mp2[N*N],cn1,cn2;
int main(){
int n;
scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
cn1 = cn2 = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
mp1[cn1++] = a[i] + b[j];
mp2[cn2++] = c[i] + d[j];
}
}
sort(mp2,mp2+cn2);
ll ans = 0;
for(int i = 0;i < cn1;i++){
int x = lower_bound(mp2,mp2+cn2,-mp1[i]) - mp2;
if(mp1[i] + mp2[x] == 0) ans += upper_bound(mp2,mp2+cn2,-mp1[i]) - mp2 - x;
}
printf("%lld\n",ans);
return 0;
}

POJ 2785 4 Values whose Sum is 0 (二分)题解的更多相关文章

  1. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  2. POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找

    2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...

  3. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  4. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  5. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

  6. POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)

    题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...

  7. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  8. POJ 2785 4 Values whose Sum is 0 Hash!

    http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

  9. poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))

    Description The SUM problem can be formulated . In the following, we assume that all lists have the ...

  10. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

    题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

随机推荐

  1. Python实现常用的逻辑数据结构

    逻辑数据结构包括:线形结构.树形结构.图形结构.集合:存储结构包括:顺序存储.链式存储.索引存储.散列存储. 同一种逻辑结构可以有四种存储结构,不同的存储结构增.删.查.改的速度不同.逻辑结构与存储结 ...

  2. 007-jdk1.6版本新特性

    一.JDK1.6 名称:Mustang(野马) 发布日期:2006-04 新特性: 1.1.AWT新增加了两个类:Desktop和SystemTray[忽略] 前者可以用来打开系统默认浏览器浏览指定的 ...

  3. ZOJ:2833 Friendship(并查集+哈希)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833 A friend is like a flower, a rose ...

  4. [LeetCode] 697. Degree of an Array_Easy tag: Hash Table

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  5. 7.4 Models -- Pushing Records into the Store

    一.概述 1. store是作为一个所有records的缓存,这些records已经被你的应用程序加载.在你的app中如果你的路由或者一个controller请求一条record,如果它在缓存中这个s ...

  6. VMware Coding Challenge: The Heist

    类似BackpackII问题 static int maximize_loot(int[] gold, int[] silver) { int[][] res = new int[gold.lengt ...

  7. img height test

  8. JavaScript DOM2

    1.Window.history:window.open打开网页的方式必须是_self window.history.back()后退 Window.history.forward()前进 <b ...

  9. SV中的随机化

    SV搭建testbench的关键概念:CRT(constraint random test),测试集的随机化. 由于对象class由数据和操作组成,所以对数据的随机化一般放在一个class内.(对环境 ...

  10. Detour的简单使用

    Detours的安装:下载部分:1.直接在百度搜"detour",进对应的网站下载.2.或以下链接https://www.microsoft.com/en-us/research/ ...