题目:

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).
 
 
题意:
给一个n*4的矩阵,输入n*4个数,在每一列找一个数,使得四个数的和为0;
 
分析:
先分别求出a和b,c和d两列任意两个数的和存放到相应的数组,将cd的和进行排序后,再用二分法进行查找;二分查找的时候注意,倘若中间的数据符合条件的话要再往两边进行查找,因为不能排除有多个数字相等的情况
 
注意:
求第二组数据的时候,根据提交的结果是不需要初始化total的;
 
 
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int a[N],b[N],c[N],d[N];
int ab[N*N],cd[N*N];
int main()
{
int n,total=,i,j;
while (cin>>n)
{
for (i=;i<n;i++)
cin>>a[i]>>b[i]>>c[i]>>d[i];
int num1=,num2=;
for (i=;i<n;i++)
for (j=;j<n;j++)
{
ab[num1++]=a[i]+b[j];
cd[num2++]=-(c[i]+d[j]);
}
sort (cd,cd+num2);
for (i=;i<num1;i++)
{
int mid,up=num2-,low=;
while (low<=up)
{
mid=low+(up-low)/;
if (ab[i]==cd[mid])
{
total++;
for (j=mid+;j<=up;j++)
{ if (ab[i]==cd[j])
total++;
else
break;
}
for (j=mid-;j>=low;j--)
{
if (ab[i]==cd[j])
total++;
else
break;
} break;
}
else
{
if (ab[i]>cd[mid])
low=mid+;
else
up=mid-;
}
}
}
cout << total << endl;
} 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. 4 Values whose Sum is 0(二分)

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

  3. POJ 2785:4 Values whose Sum is 0 二分

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

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

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

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

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

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

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

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

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

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

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

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

  10. 二分-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. 将keras的h5模型转换为tensorflow的pb模型

    h5_to_pb.py from keras.models import load_model import tensorflow as tf import os import os.path as ...

  2. JavaEE--调用 WSDL -- httpclient 4.x.x

    参考:http://aperise.iteye.com/blog/2223454   http://blog.csdn.net/chenleixing/article/details/43456987 ...

  3. MySQL--启动和关闭MySQL服务

    1.Windows下 启动服务 mysqld --console 或 net start mysql 关闭服务 mysqladmin -uroot shudown 或 net stop mysql 2 ...

  4. 小程序外链跳转web-view系列问题

    1.当小程序需要跳转外链时要上小程序后台配置业务域名,配置业务域名需要上传一个验证文件到你跳转的外链的服务器上的根目录里: 2. (1)第一种方法:.当小程序在同一个页面根据后台接口获取的url进行小 ...

  5. mysql自定义函数初始化数据:init_data()

    DELIMITER $$ USE `local_hnyz`$$ DROP FUNCTION IF EXISTS `init_data`$$ CREATE DEFINER=`root`@`localho ...

  6. 898B. Proper Nutrition#买啤酒问题(枚举&取余)

    题目出处:http://codeforces.com/problemset/problem/898/B 题目大意:为一个整数能否由另外两个整数个整数合成 #include<iostream> ...

  7. mac环境下创建bash_profile文件并写入内容 更改php环境变量

    1. 启动终端Terminal 2. 进入当前用户的home目录 输入cd ~ 3. 创建.bash_profile 输入touch .bash_profile 4. 编辑.bash_profile文 ...

  8. K - The Unique MST (最小生成树的唯一性)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  9. centos telnet

    yum install telnet yum install telnet-server

  10. day03-函数

    形参:位置参数:必须传 *args:动态参数,可以接收任意多个位置参数 默认值参数:可以传也可以不传 **kwargs:动态参数,可以接收多个关键字参数. 实参:按照位置传参,按照关键字传参. #顺序 ...