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

俩个数组分别装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. 深度学习1--ubuntu14.04+win10双系统

    相当多的内容参考的百度经验https://jingyan.baidu.com/article/eb9f7b6d79a7b4869364e885.html?qq-pf-to=pcqq.group 还可以 ...

  2. 【LeetCode】 子字符串思路

    在一些求字串含有固定字符最短串,含有不同字符最长子串等问题中,利用 vector<int> map(128, 0)可以解决 题一:最短给定子串 Minimum Window Substri ...

  3. 每天一个Linux命令(50)netstat命令

        netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况.     (1)用法:     用法:  netstat [选项参数]     (2)功能: ...

  4. Redis之数据存储结构

    今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...

  5. 跨平台移动开发_Android 平台使用 PhoneGap 方法

    PhoneGap  下载地址http://phonegap.com/install/    1.打开 Eclipse,在文件菜单下面点击 创建> Android Application Proj ...

  6. POJO、Bean和JavaBean

    本文总结自: https://blog.csdn.net/chenchunlin526/article/details/69939337 POJO (plain pld java object) 一个 ...

  7. WebUploader API文档

    Web Uploader内部类的详细说明,以下提及的功能类,都可以在WebUploader这个变量中访问到. As you know, Web Uploader的每个文件都是用过AMD规范中的defi ...

  8. python编程常见小技巧

    #主要是记录常见的小问题以及解决办法 ##1.复制的代码,经常出现TAB和空格不一致的情况 将tab或者空格删除,然后重新打出空格或者tab就可以了: ##2.python读取文件,经常出现的编码en ...

  9. hibernate集合的加载策略

    在集合方配置lazy和fetch的方式,默认的是lazy为true,fetch为select,lazy有true,extra和false,true和extra都是懒加载,只是extra比true更懒, ...

  10. Tensorflow 从零开始

    1.安装pip pip是一个用于管理和安装Python包的工具,类似于LINUX 的yum命令一样! 命令(Ubuntu系统):sudo apt-get install python-pip 验证安装 ...