4 Values whose Sum is 0
Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 14475   Accepted: 4138
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

 
折半枚举,然后二分。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX = ; typedef long long ll; int N;
int a[][MAX];
int cd[MAX * MAX]; void solve() {
ll res = ; for(int i = ; i <= N; ++i) {
for(int j = ; j <= N; ++j) {
cd[(i - ) * N + j] = a[][i] + a[][j];
}
}
sort(cd + ,cd + N * N + ); for(int i = ; i <= N; ++i) {
for(int j = ; j <= N; ++j) {
int x = -(a[][i] + a[][j]);
res += upper_bound(cd + ,cd + N * N + ,x) -
lower_bound(cd + ,cd + N * N + ,x); }
} printf("%I64d\n",res); } int main()
{
// freopen("sw.in","r",stdin);
scanf("%d",&N);
for(int i = ; i <= N; ++i) {
for(int j = ; j <= ; ++j) {
scanf("%d",&a[j][i]);
}
} solve(); return ;
}

POJ 2785的更多相关文章

  1. POJ 2785 HASH

    题目链接:http://poj.org/problem?id=2785 题意:给定n行数字,每行4个数分别是a,b,c,d,现在要求能有多少个(a,b,c,d)组合并且和为0 思路:n^2统计所有(a ...

  2. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

    题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

  3. poj 2785 让和为0 暴力&二分

    题目链接:http://poj.org/problem?id=2785 大意是输入一个n行四列的矩阵,每一列取一个数,就是四个数,求有多少种着四个数相加和为0的情况 首先脑海里想到的第一思维必然是一个 ...

  4. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  5. POJ 2785 4 Values whose Sum is 0 Hash!

    http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

  6. poj 2785(折半枚举+二分搜索)

    传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...

  7. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  8. Divide and conquer:4 Values whose Sum is 0(POJ 2785)

    找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...

  9. POJ 2785 4 Values whose Sum is 0

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

随机推荐

  1. 《高性能javascript》读书笔记

    1.每个<script>加载时都会阻塞其他文件(图片.音乐等)的同时加载,同时浏览器会在js代码执行时停止渲染Dom.所以为了减少界面加载的卡顿和空白发生,应尽力将js代码或者文件放在&l ...

  2. Python input()

    在Python语言中,我们经常需要与用户实现交互,下面是一个小实例 # -*- coding:UTF-8 -*- #获取输入参数,并将输入的值存储到txt文件中 String1 = input(&qu ...

  3. 通过DB_LINK按照分区表抽取数据

    DB:11.2.0.3.0OS:oracle-linux 5.7 建表语句:create table YOON.YOON_HIS(  c_id              NUMBER not null ...

  4. 编写可维护的JavaScript之简易模版

    /* * 正则替换%s * @para arg1(text) 需要替换的模版 * @para arg2 替换第一处%s * @para arg3 替换第二处%s * 返回替换后的字符串 */ var ...

  5. 3.Knockout.Js(属性绑定)

    前言 让visible绑定到DOM元素上,使得该元素的hidden或visible取决于绑定的值. 简单的绑定 首先还是先定义一个ViewModel var AppViewModel = { shou ...

  6. 可综合风格的VerilogHDL模块实例

    1.赋值语句:assign{cout,sum}=a+b+cin; 2.利用电平敏感的always块设计组合逻辑电路 3.always块中如果含有局部变量,就必须在begin后加模块名,是必须加,同样的 ...

  7. [原]项目进阶 之 集群环境搭建(二)MySQL集群

    上次的博文中我们介绍了一下集群的相关概念,今天的博文我们介绍一下MySQL集群的相关内容. 1.MySQL集群简介 MySQL群集技术在分布式系统中为MySQL数据提供了冗余特性,增强了安全性,使得单 ...

  8. NSNumber、NSValue、NSDate、NSObject

    注:OC中数组和字典只能存储OC对象不能存放基本数据类型. NSNumber NSNumber可以用来把一个基本数据类型包装成一个NSNumber类型的对象. NSNumber *number = [ ...

  9. 团队作业php

    <?php$kouwei=$_GET["select"];$daxiao=$_GET["RadioGroup1"];$peiliao=$_GET[&quo ...

  10. 阿里云服务器mysql修改编码问题

    最近在学习struts+spring+hibernate,强烈推荐新手一本书:陈天河<轻量级web应用开发>,这本书是我见过的国内最好的书,初学者可以买本读读. 不说这个了,来说说我的问题 ...