4 Values whose Sum is 0

Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 21370   Accepted: 6428
Case Time Limit: 5000MS

Description

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 228 ) 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).

Source

Southwestern Europe 2005

// 题意: 比如例子 6 行,每行 4 个数,从第一,二,三,四列各选一个数,要使和为 0 ,有几种组合?

n 最大有4000,但还是可以暴力,加二分

a + b = -( c + d )

枚举 a + b ,对于 c + d ,枚举一次,用一个大数组保存和,sort ,就可以二分了。要注意的是,二分到了要对左右继续搜索一下,不同的位置代表有不同的组合

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; int n;
int k; int num[][];
int s[]; int erfen(int t)
{
int l=,r=k-;
while (l<=r)
{
int mid=(l+r)/;
if (s[mid]==t)
{
int e = mid-;
int all=;
while (e>=&&s[e]==t)
e--,all++;
e=mid+;
while (e<k&&s[e]==t)
e++,all++;
return all;
}
else if (s[mid]>t)
r=mid-;
else
l=mid+;
}
return ;
} int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<n;i++)
scanf("%d%d%d%d",&num[i][],&num[i][],&num[i][],&num[i][]);
k=;
for (int i=;i<n;i++)
for (int j=;j<n;j++)
s[k++]=-(num[i][]+num[j][]);
sort(s,s+k);
int total=;
for (int i=;i<n;i++)
{
for (int j=;j<n;j++)
{
int left=num[i][]+num[j][];
total+=erfen(left);
}
}
printf("%d\n",total);
}
return ;
}

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 二分

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

  3. POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找

    2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...

  4. UVA - 1152 --- 4 Values whose Sum is 0(二分)

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  5. POJ2785 4 Values whose Sum is 0 (二分)

    题意:给你四组长度为\(n\)序列,从每个序列中选一个数出来,使得四个数字之和等于\(0\),问由多少种组成情况(仅于元素的所在位置有关). 题解:\(n\)最大可以取4000,直接暴力肯定是不行的, ...

  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. 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< ...

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

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

  9. 二分-G - 4 Values whose Sum is 0

    G - 4 Values whose Sum is 0 The SUM problem can be formulated as follows: given four lists A, B, C, ...

随机推荐

  1. ylb: 数据库操作方法基础

    ylbtech-SQL Server:SQL Server-数据库操作方法基础 数据库操作方法基础. ylb: 数据库操作方法基础 返回顶部 ----------试图操作(view)--------- ...

  2. 淘宝开源项目之Tsar

    软件介绍: Tsar是淘宝开发的一个非常好用的系统监控工具,在淘宝内部大量使用,它不仅可以监控CPU.IO.内存.TCP等系统状态,也可以监控Apache,Nginx/Tengine,Squid等服务 ...

  3. Spark map-side-join 关联优化

    在spark中要进行join操作,如果在shuffle的时候进行join效率较低.如果满足 所需要join的表中有一张表较小,那么可以考虑在map端进行join操作. 转载:http://blog.c ...

  4. 转: 使用 Velocity 模板引擎快速生成代码

    from:https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ 评注: 1. velocity 的基本语法 2. 生成代码的用法.

  5. 转: android 实现效果特效

    一. 各种控件效果 http://www.jcodecraeer.com/plus/list.php?tid=31&TotalResult=990&PageNo=7

  6. sql 查询重复的数据

    select * from yryz_role_partner where user_id in (select user_id from yryz_role_partner group by use ...

  7. js - 正斜杆网址转换

    2018-8-7(更新) let http = 'http://www.baidu.com/com/img/1.jpg'; let https = 'http:\\www.baidu.com\\com ...

  8. node - 上传文件并且修改名称

    html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  9. iOS开发:解决UIScrollView不滚动的问题

    照着书上的Demo(iOS 5.0的教程),在- (void)viewDidLoad里设置scrollView的contentsize,让它大于屏幕的高度,却发现在模拟器中没用,还是不能滚.经过 一翻 ...

  10. java中常用的类型转换

    1.将字符串转换成整数(String--->int)方法一: (1)  int i = Integer.parseInt(String s);  其中(1)其实就是我们经常用到的将s转换为10进 ...