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

俩个数组分别装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. PHP/Yii2操作Cookie,常见问题以及注意事项

    设置Cookie PHP setcookie("name", "Larry",time()+3600 Yii2 $cookies = Yii::$app-> ...

  2. 小程序学习第二天 认识框架WXML

    一.初级小程序HelloWorld 心得: (1)progect.config.json :app的个性化设置 (2)一个小程序至少包括两个文件 (2.1)app.json 小程序全局配置       ...

  3. Python自然语言处理 - 系列四

    一 如何使用形式化语法来描述无限的句子集合的结构?    --上下位无关文法       1.1 一个例子:      grammar1 = nltk.parse_cfg(""&q ...

  4. OCI编程

    OCI编码步骤: .定义OCI数据结构 .连接到Oracle数据库:使用orlon函数调用还可以并发连接多个数据库. .打开光标.通过调用oopen来打开一个光标. .分析语句.使用oparse来分析 ...

  5. HGVS的变异格式

    符号: 1.HGVS的变异格式由两部分组成: 1.1 reference sequence file identifier (accession.version-number) :  actual d ...

  6. Android系统属性SystemProperties在应用层的用法【转】

    本文转载自:https://blog.csdn.net/lilidejing/article/details/53288243 如果你看到这篇文章了,说明你已经是资深程序员,会发现整个Android系 ...

  7. 关于C++ 中的this 的理解

    关键字this 通常被用在一个class内部,指正在被执行的该class的对象(object)在内存中的地址.它是一个指针,其值永远是自身object的地址.

  8. ResourceBundle和properties 读取配置文件区别

    java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Properties同时还能用来写文 ...

  9. Complex social network Partition for Balanced Subnetworks---Hao Lan Zhang,Jiming Liu,Chunyu Feng,Chaoyi Pang,Tongliang Li,Jing He阅读

    摘要:Abstract—Complex social network analysis methods have been applied extensively in various domains ...

  10. mysql一次运行多个SQL文件

    在文件 batch.sql 中写下多个SQL文件 source file1.SQLsource file2.SQLsource file3.SQL 然后运行 source batch.sql