个人心得:单纯用二分法一直超时,后面发现我的那种方法并没有节省多少时间,后面看了大神的代码,真的是巧妙,

俩个数组分别装a+b,c+d。双指针一个指向最后,从第一个开始想加,加到刚好大于0停止,再看是否存在和为0的情况。

很巧妙,因为此时i,j所指想加刚好大于0,因为是排完序的,所以i往后面走的时候,大于j的数相加一定大于0,所以卡的非常好;

就没有再指针跳转回去了,佩服!

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

5

Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#define maxn 4004
using namespace std;
int map1[maxn*maxn];
int map2[maxn*maxn];
int a[maxn],b[maxn],c[maxn],d[maxn];
int main()
{
int n,i,j,k,sum,p;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
}
for(i=;i<n;i++)
for(j=;j<n;j++)
map1[i*n+j]=a[i]+b[j];
for(i=;i<n;i++)
for(j=;j<n;j++)
map2[i*n+j]=c[i]+d[j];
sort(map1,map1+n*n);
sort(map2,map2+n*n);
sum=;
p=n*n-;
for(i=;i<n*n;i++)
{
while(p>=&&map1[i]+map2[p]>) p--;
if(p<) break;
int temp=p;
while(temp>=&&map1[i]+map2[temp]==)
{
sum++; temp--;
}
}
printf("%d\n",sum);
//system("pause");
return ;
}

The Sum of 0 for four numbers(拆解加二分思想)的更多相关文章

  1. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

  2. find sum and average of n numbers

    public class Solution { public static void main(String[] args) { Scanner ip = new Scanner(System.in) ...

  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. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

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

  6. [poj2785]4 Values whose Sum is 0(hash或二分)

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

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

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  8. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

  9. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

随机推荐

  1. web测试点梳理

    前言 前面一篇文章讲解了app测试一些功能点.那么相应的也梳理一下web测试相关的功能的测试点吧,此篇文章只是给你们一个思路,如果要涉及web端每个测试点,基本不可能实现的,所以只是提供一个设计的思路 ...

  2. React Native中组件的props和state

    一.组件的属性(props)和状态(state) 1.属性(props) 它是组件的不可变属性(组件自己不可以自己修改props). 组件自身定义了一组props作为对外提供的接口,展示一个组件时只需 ...

  3. python的一些内置函数

    最近看到一些人写的文章里有提到python的描述符__get__,__set__,__del__. 这里我也小小研究了一下,除了这3个之外还加上过程中学习的几个,比如__call__等. __get_ ...

  4. 输入框去除默认的文字,jquery方法

    需求:所有的输入框获取焦点时,去掉默认的提示文字,失去焦点时如果输入框为空,恢复默认的提示文字. 解决方案:jquery方法,以下有三种,按照利弊,我建议最后一种. 先看html代码: <inp ...

  5. linux上不能显示Jfreechart的图片文件

     出现错误: Jan 23, 2015 4:19:21 PM org.apache.catalina.core.StandardWrapperValve invokeSEVERE: Servlet.s ...

  6. win7下users用户文件转移到其他盘符

    在安装完的系统中, 1.重启按F8键到修复系统界面: 2.下一步->下一步->运行dos窗口,打开dos窗口: 3.拷贝命令:robocopy "C:\Users" & ...

  7. 【转】Android ImageView的scaleType属性与adjustViewBounds属性

    ImageView的scaleType的属性有好几种,分别是matrix(默认).center.centerCrop.centerInside.fitCenter.fitEnd.fitStart.fi ...

  8. 防域名DNS劫持 从保护帐号安全做起

    什么攻击能造成区域性的网络瘫痪?没错,DNS劫持.这个堪称核武器的攻击方式,一旦爆炸,后果不堪设想.2014年1月21日,全国大范围出现DNS故障,下午,中国顶级域名根服务器出现故障,大部分网站受影响 ...

  9. JavaScript -- 广告随鼠标移动, 点击一次后关闭

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. linux vim vi编辑时撤销输入操作

    linux vim vi编辑时撤销输入操作 1,esc退出输入状态 2,u 撤销上次操作 3,ctrl+r 恢复撤销