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. Linux下系统信息工具之Saidar

    Saidar,是一款命令行下的小工具,可用来查看CPU.进程.负载.内存.交换分区.网络.磁盘.文件系统等系统信息. #ubuntu sudo apt-get install saidar -y #f ...

  2. Vue表单和组件

    一.表单 v-model 指令在表单控件元素上创建双向数据绑定,v-model 会根据控件类型自动选取正确的方法来更新元素. <input v-model="message" ...

  3. windows 用wireshark抓本机的包

    原文: http://bijian1013.iteye.com/blog/2299856 1.也可以用另外一个工具: RawCap 当然也不是说windows下就别想抓到本地回路的包了,肯定有别的方法 ...

  4. 2017.4.12 开涛shiro教程-第十八章-并发登录人数控制

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十八章-并发登录人数控制 shiro中没有提 ...

  5. [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)

    While sometimes outside input can have influence on how a given stateful transaction transitions, th ...

  6. [Algorithms] Classify Mystery Items with the K-Nearest Neighbors Algorithm in JavaScript

    The k-nearest neighbors algorithm is used for classification of unknown items and involves calculati ...

  7. 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 21.0怎么办

    编码器通讯断线异常保护,一般就是通讯线松动或者受干扰(最难以排查的情况是,我接了六套驱动器和伺服,比如J0的线是随便做的,其他五套都是西门子的合格网线,我运行程序的时候,J0如果单关节运动没任何问题, ...

  8. iOS11

    _tab.estimatedRowHeight = 0; if (@available(iOS 11.0, *)) { //当有heightForHeader delegate时设置 _tab.est ...

  9. Java BaseDao

    BaseDao类: package dao; import java.sql.*; public class BaseDao { private static final String driver ...

  10. Php 中如何将内容写入log日志中

    $Str = '你想要存的内容'; file_put_contents('abc.log', "\r\n\r\n". $Str, FILE_APPEND);     FILE_AP ...