思路:

如果用朴素的方法算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. idea启动java Maven项目,出现" java: 程序包xxxx不存在"

    今天运行Maven项目的时候,出现了,Error:(19, 17) java: 程序包tracetool不存在的情况 本人的解决办法: (1)首先确保maven  pom文件不能报错,即文件上面不能有 ...

  2. MySQL找出锁等待

    1.服务器级别的锁等待 可以通过show processlist看到等待锁的线程id,但是无法知道究竟哪个线程持有锁 可以通过mysqladmin debug 相关等待锁的线程以及谁持有锁可以在错误日 ...

  3. /etc/ssh/sshd_config

    线上配置: Port 49142 # 设置SSH监听端口 RSAAuthentication yes # 开启RSA密钥验证,只针对SSHv1 PubkeyAuthentication yes # 开 ...

  4. Agreementhasbeenupdated--EditPhoneNumber

    1.https://appleid.apple.com/#!&page=signin 登录appid账号  在账户里面的编辑 添加更多信息,新增手机号 2.如果不可以 可以试下这个https: ...

  5. Centos安装ELK5.3.2

    一.注意情况 1.elk的版本要一致. 2.ElasticSearch是基于lucence开发的,也就是运行需要java支持.所以要先安装JAVA环境.由于es5.x依赖于JDK1.8,所以需要安装J ...

  6. Java-二分查找算法

    package com.lym.binarySearch; import java.util.Arrays; /** * 二分查找 * * @author Administrator * */ pub ...

  7. C#可扩展数组转变为String[]数组

    简单备忘: 由于需要将数据最终以逗号隔开来拼接,因而写了下面的处理方法. public void GetJoinString() { ArrayList arr = new ArrayList(); ...

  8. java 事件通告写法

    使用场景: 自己模块发生变化后可能引起其他模块变化的部分,需要添加事件通告,通知其他模块. 使用模式: 观察者模式 (以User类为例)写法: 1.创建接口: public interface IUs ...

  9. ac1008

    这题说的是 给了n个点在圆 上 然后 i连 i+2 从i+2 开始连 i+4  然后 这样一直到某个点 已经被连过为止   如果还有的没有被连过 就从那个点开始 连 按照上面的规则 当 N大于6 的时 ...

  10. 持续集成之二:搭建SVN服务器(整合Apache+Subversion)

    安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 rhel-server-7.3-x86_64-dvd.iso ...