题目:

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. Tensorflow学习教程------tfrecords数据格式生成与读取

    首先是生成tfrecords格式的数据,具体代码如下: #coding:utf-8 import os import tensorflow as tf from PIL import Image cw ...

  2. 题解【语文1(chin1)- 理理思维】

    link 喵~珂朵莉树AC 珂朵莉树?见此处~ 这数据结构太暴力了,所以不讲了 Code: #include<iostream> #include<cstdio> #inclu ...

  3. 关于SG函数

    Sprague-Grundy定理(SG定理): 游戏和的SG函数等于各个游戏SG函数的Nim和.这样就可以将每一个子游戏分而治之,从而简化了问题.而Bouton定理就是Sprague-Grundy定理 ...

  4. CodeForces 1006E Military Problem(DFS,树的选择性遍历)

    http://codeforces.com/contest/1006/problem/E 题意: 就是给出n,m,共n个点[1,n],m次询问.第二行给出n-1个数a[i],2<=i<=n ...

  5. python 多维数组 字典

    #多维数组list = [[87,57,98],[34,87,90]]print(list[1][0]) l = [[87,57,98],[34,87,[90,99,67]]]print(l[1][2 ...

  6. 01 - CentOS 中安装Python 2.7.16

    准备 下载链接:https://www.python.org/ftp/python/ 下载源码:wget https://www.python.org/ftp/python/2.7.16/Python ...

  7. scala编程(九)——控制抽象

    减少代码重复 所有的函数都被分割成通用部分,它们在每次函数调用中都相同,以及非通用部分,在不同的函 数调用中可能会变化.通用部分是函数体,而非通用部分必须由参数提供.当你把函数值用做参数时,算法的非通 ...

  8. 关于 Xpath 定位

    关于 Xpath 定位 问: // 和 / 的区别 表达式 描述 nodename 选取此节点的所有子节点. / 从根节点选取. // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置. . ...

  9. TOJ-3474 The Big Dance(递归二分)

    链接:https://ac.nowcoder.com/acm/contest/1077/L 题目描述 Bessie and the herd, N (1 <= N <= 2,200) co ...

  10. ASP.NET防止连续多次点击提交按钮 导致页面重复提交

    PS:实际使用中发现,第①种方法在火狐浏览中有时候有问题.第2种方法,在各个浏览器中都没问题 近做项目遇到了这样的情况: 公司网络比平常慢了不少,在点击保存按钮提交页面后需等待挺长的一段时间,忍不住手 ...