G - 4 Values whose Sum is 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).
  题目大意:每行会给出四个整数,要求在每列整数中找出一个整数,使四个整数之和为0,求有几种不同组合
 #include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
const int maxm = *;
int a[maxn],b[maxn],c[maxn],d[maxn];
int ab[maxm],cd[maxm]; int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d %d %d %d",a+i,b+i,c+i,d+i); int cnt = ;
for(int i=;i<n;i++)//前两列整数求和
for(int j=;j<n;j++)
ab[cnt++] = a[i] + b[j];
cnt = ;
for(int i=;i<n;i++)//后两列整数求和
for(int j=;j<n;j++)
cd[cnt++] = c[i] + d[j]; sort(ab,ab+n*n);
sort(cd,cd+n*n); int p = n*n-;
cnt = ;
for(int i=;i<n*n;i++){
for(;p>=;p--)
if(ab[i]+cd[p]<=) break;
if(p<) break;
for(int j=p;j>=;j--){
if(ab[i]+cd[j]==) cnt++;
if(ab[i]+cd[j]<) break;
}
}
printf("%d",cnt);
}

  面对四列整数,一一组合将会造成极其庞大的数据,可将其中两两组合,组合以后再相加寻找和为0的情况。

二分-G - 4 Values whose Sum is 0的更多相关文章

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

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

  2. POJ - 2785 4 Values whose Sum is 0 二分

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

  3. 4 Values whose Sum is 0(二分)

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

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

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

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

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

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

  7. UVA1152-4 Values whose Sum is 0(分块)

    Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time Limit: 9000 mSec Problem Desc ...

  8. POJ:2785-4 Values whose Sum is 0(双向搜索)

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

  9. 4 Values whose Sum is 0 POJ - 2785

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

随机推荐

  1. HTML连载67-手风琴效果、2D转换模块

    一.手风琴效果 <style> *{ margin:0; padding:0; } ul{ width: 960px; height: 300px; margin:100px auto; ...

  2. go 网络编程

    网络编程 tcp 1.tcp客户端服务端实现 server/server.go package main import ( "fmt" "net" ) /* t ...

  3. 在阿里云服务器上安装mysql及重置密码

    参考链接:https://www.cnblogs.com/ljysy/p/10324854.html 下载与安装MySQL: 这里采用Yum管理好了各种rpm包的依赖,能够从指定的服务器自动下载RPM ...

  4. PostgreSQL内核学习笔记四(SQL引擎)

    PostgreSQL实现了SQL Standard2011的大部分内容,SQL处理是数据库中非常复杂的一部分内容. 本文简要介绍了SQL处理的相关内容. 简要介绍 SQL文的处理分为以下几个部分: P ...

  5. WPF 控件功能重写(ComboBox回车搜索)

    前言:在我们日常使用软件的时候,Combobox会让用户很方便的选择出需要的东西,但是ComboBox中的下拉行数过多时就不那么好用了. 如果在项目中有很多这样的ComboBox控件的话,我们可以考虑 ...

  6. 第3种方法获取redis cluster主从关系

    需求:使用cluster slots命令,获取redis cluster 主从对应关系. 说明:cluster slots命令对应的字段说明:http://redis.cn/commands/clus ...

  7. 【39】为什么使用卷积?(Why convolutions?)

    为什么使用卷积?(Why convolutions?) 我们来分析一下卷积在神经网络中如此受用的原因,然后对如何整合这些卷积,如何通过一个标注过的训练集训练卷积神经网络做个简单概括.和只用全连接层相比 ...

  8. 将一个Head下的Line复制到另一个Head下(ef+linq)

    今天工作中有一个需求,要求将一个Item下的Line复制到另外一个Item下面 这个需求在工作中很多,按照以往的经验肯定是先delete from,然后再一条条遍历后insert into 这两天正好 ...

  9. Page Object设计模式(二)——poium测试库

    一.简介 poium是一个基于Selenium/appium的Page Object测试库,最大的特点是简化了Page层元素的定义. 项目地址:https://github.com/SeldomQA/ ...

  10. Qt读写文件

    1.头文件 #include<QFile> #include<QFileDialog> #include<QDataStream> 2.写代码前工作 在ui界面拖入 ...