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;
 ;
 ;
 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);
         ;i<n;i++)
             scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
         ;
         ;i<n;i++)
             ;j<n;j++)
                 sum[cnt++]=A[i]+B[j];
         sort(sum,sum+cnt);
         LL ans=;
         ;i<n;i++)
             ;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");
     }
     ;
 }

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

  1. BAPC2014 K&amp;amp;&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. bash学习

    if 的使用 declare -i x=10 if [ $x -lt 14 ];then echo $x'小于14'; elif [ $x -gt 9 ];then echo $x'大于9'; els ...

  2. springmvc+mybatis事务回滚

    spring-mybatis.xml中 配置了 <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" tran ...

  3. (九)play之yabe项目【发表博文】

    (九)play之yabe项目[发表博文] 博客分类: 框架@play framework   发表一篇博文 填充管理页面 从主页链接到管理页面时,只简单显示了登陆用户的名称 现在对显示的内容加以丰富 ...

  4. [Tyvj模拟赛]运

    运 题目 [问题背景]zhx 和妹子们玩数数游戏. [问题描述] 仅包含4或7的数被称为幸运数.一个序列的子序列被定义为从序列中删去若干个数, 剩下的数组成的新序列.两个子序列被定义为不同的当且仅当其 ...

  5. js进度条小事例

    <style> #div1{width: 500px;height: 20px;border: 1px solid gray;} #div2{height: 20px;width: 0px ...

  6. 用ASP.NET Core 2.0 建立规范的 REST API -- DELETE, UPDATE, PATCH 和 Log

    本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...

  7. git常用方法整理

    Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 初始化本地仓库 mkdir xxx cd xxx git init 创建本 ...

  8. 常用LINUX命令汇总

    一.基本命令bash Bash(GNU Bourne-Again Shell)是许多Linux平台的内定Shellpwd 查看当前所在目录ls 查看目录内所有文件cd 进入目录cd .. 返回上一层p ...

  9. ubuntu16.4菜单栏不见,终端不见解决方法

    1.ctrl+alt+f1进入命令行 2. sudo apt-get install gnome-terminal 3.sudo apt-get install unity 4.setsid unit ...

  10. rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted

    今天在同步数据的时候提示rsync: failed to set times on “xxxx”: Operation not permitted,一般来说要不是服务器时间不对或者权限没有设置好,下面 ...