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. 转】MyEclipse使用总结——修改MyEclipse默认的Servlet和jsp代码模板

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3769058.html 感谢! 一.修改Servlet的默认模板代码 使用MyEclipse创建Servlet时, ...

  2. Determining Equality of Objects

    [Determining Equality of Objects] If you need to determine whether one object is the same as another ...

  3. [原创]Devexpress XtraReports 系列 8 创建Drill-Through报表

    哎,今天公司工作忙了一天,一直没有时间写写东西.所以只能昨天晚上加班写咯.苦逼啊...... 昨天发表了Devexpress XtraReports系列第七篇[原创]Devexpress XtraRe ...

  4. codeforces 644A Parliament of Berland

    A. Parliament of Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. yii缓存设置使用

    'filecache'=>array( 'class'=>'system.caching.CFileCache', 'directoryLevel'=>'3',), //在main. ...

  6. javascript 汉字生成拼音

    在网上下载的一个汉字生成拼音的js,很有用,大家一起分享! var PinYin = {"a":"/u554a/u963f/u9515","ai&qu ...

  7. Window.navigator

    定义和用法 userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. 一般来讲,它是在 navigator.appCodeName 的值之后加上斜线和 navig ...

  8. 组合View Controller时遇到的一点问题

    View Controller的组合应用其实很常见了,比如说Tab bar controller和Navigation view controller的组合使用,像这种一般都是Navigation v ...

  9. jquery表格伸展

    1:效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org ...

  10. 关于新建JSP文件后,文件开头报错的处理

    新建了一个web工程,之后建立了jsp页面,刚建立完成,文件开头就报错:The superclass "javax.servlet.http.HttpServlet" was no ...