思路:

如果用朴素的方法算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. linux中fork函数详解(转)

    add by zhj: 在Linux,创建进程是用fork(),它其实就是拷贝父进程的数据段和其它数据,这相当于C函数调用中的值传递,这是 此后两者的修改都互不影响.因为两者的数据虽相同,但却在不同的 ...

  2. django源码笔记-【2】(转)

    add by zhj: 在原文的基础上有修改 原文:http://www.cnblogs.com/gaott/archive/2012/02/28/2371238.html 上一期完理解了WSGI处理 ...

  3. grunt学习一

    grunt是前端自动化工具之一.下面是是grunt的简单小示例: 在使用grunt,确保安装nodejs,如果不清楚,可以百度找相关教程,这个教程已经烂大街了. 1.打开cmd,以管理员的身份.(或者 ...

  4. ssh stricthostkeychecking=0

    SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击. 但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. ...

  5. SQL Server 安装后改动计算机名带来的问题以及解决方法

    USE master GO DECLARE @serverproperty_servername varchar(100), @servername varchar(100) --取得Windows ...

  6. display:table和display:table-cell结合使用

    .GoodList{ display :table; height :54px; width :56px; line-height: 14px                 padding: 0 1 ...

  7. Spark Streaming性能优化: 如何在生产环境下应对流数据峰值巨变

    1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...

  8. Ubuntu单用户模式(安全模式)

           说下我遇到的情况,ubuntu服务器,防火墙关闭,连的外网.服务器中毒,病毒自动生成用户,然后病毒进程开启启动,进程启动后,cpu立马占满,服务器立马卡死,本想着服务器启动后通过top命 ...

  9. ID和Name

    ID和Name都可以用来标识一个标记,Javascript分别有两个方法getElementById和getElementByName来定位Dom节点. 区别如下: 1.我们知道在网页做Post提交时 ...

  10. JS的增删改查

    1.查 <script type="text/javascript"> /** * 查找 已经在html代码中存在的元素 */ /** * document.getEl ...