K - 4 Values whose Sum is 0

Crawling in process... Crawling failed Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description:
System Crawler (2015-03-12)

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 ) AxBxCxD are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

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 test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

1

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

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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
int A[MS],B[MS],C[MS],D[MS],n,sum[MS*MS]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
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++)
sum[cnt++]=A[i]+B[j];
sort(sum,sum+cnt);
LL ans=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
ans+=upper_bound(sum,sum+cnt,-C[i]-D[j])-lower_bound(sum,sum+cnt,-C[i]-D[j]);
}
printf("%lld\n",ans);
if(T)
printf("\n");
}
return ;
}

K - 4 Values whose Sum is 0(中途相遇法)的更多相关文章

  1. BAPC2014 K&amp;&amp;HUNNU11591:Key to Knowledge(中途相遇法)

    题意: 有N个学生.有M题目 然后相应N行分别有一个二进制和一个整数 二进制代表该同学给出的每道题的答案.整数代表该同学的答案与标准答案相符的个数 要求推断标准答案有几个,假设标准答案仅仅有一种.则输 ...

  2. UVA-1152-4 Values whose Sum is 0---中途相遇法

    题目链接: https://cn.vjudge.net/problem/UVA-1152 题目大意: 给出4个数组,每个数组有n个数,问有多少种方案在每个数组中选一个数,使得四个数相加为0. n &l ...

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

  4. uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

    用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...

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

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

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

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

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

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

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

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

  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. static_cast, dynamic_cast, const_cast探讨

    转自:http://www.cnblogs.com/chio/archive/2007/07/18/822389.html 首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 ...

  2. python 加密解密

    1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...

  3. 利用UIImagePickerController或者利用UIKit的 UIGraphicsBeginImageContext保存图片

    转载自:http://my.oschina.net/hmj/blog/99970    应用中有时我们会有保存图片的需求,如利用UIImagePickerController用IOS设备内置的相机拍照 ...

  4. 陕西中际现代:基于自适应算法的PLC滴灌控制系统

    基于自适应算法的PLC滴灌控制系统 陕西中际现代包装科技有限公司滴灌部 1.介绍 水资源正在成为一种珍贵的资源.城镇的市民使用成千上万立方的水来浇灌花园和绿地.他们依赖于使用固定灌溉计划的控制器.而这 ...

  5. HDU1003前导和

    简单维护前导和 #include<stdio.h> int main() { ],cas,key=; scanf("%d",&cas); while(cas-- ...

  6. HDU1963Investment(DP)

    简单DP,题解见代码

  7. .NET文件上传的大小限制配置

    <system.web>  <!--maxRequestLength单位是Kb-->  <httpRuntime maxRequestLength="20971 ...

  8. Giraph之SSSP(shortest path)单机伪分布运行成功

    所遇问题:Exception 1: Exception in thread "main" java.lang.IllegalArgumentException: "che ...

  9. QT输入输出(一) 之 QDataStream 测试

    QT提供了两个高级别的流类---QDataStream和QTextStream,可以从任意的输入输出设备读取或写入数据. QDataStream用于读写二进制数据,它的优点是:在读写数据的时候已经严格 ...

  10. 在sql中使用了 hashbytes 函数

    在做项目的时候,在sql中使用了hashbytes函数,运用md5算法去加密一密码.代码如下 DECLARE @psw nvarchar(20) SET @psw = 'admin' SELECT h ...