J 中途相遇法,求和

---恢复内容开始---

J - 中途相遇法

Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

The SUM problem can be formulated as follows: given four lists ABCD<tex2html_verbatim_mark> of integer values, compute how many quadruplet (abcd ) AxBxCxD<tex2html_verbatim_mark> are such that a + b + c + d = 0<tex2html_verbatim_mark> . In the following, we assume that all lists have the same size n<tex2html_verbatim_mark> .

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<tex2html_verbatim_mark> (this value can be as large as 4000). We then have n<tex2html_verbatim_mark> lines containing four integer values (with absolute value as large as 228<tex2html_verbatim_mark> ) that belong respectively to ABC<tex2html_verbatim_mark> and D<tex2html_verbatim_mark> .

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

题目思路:

大意:每列找一个数,得到和为0的序列,有几种不同的方案

分析:

如果进行四重循环,毫无疑问会超时,这时需要用到枚举案例,

对1,2列的数求一个和,3,4列的数求一个和,然后进行二分查找

程序代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int a[16000010];
int b[16000010];
int p[4010][4];
int t;
int erfen(int x)
{
    int cnt=0;
    int l=0,r=t-1,mid;
    while(r>l)
    {
        mid=(l+r)>>1;
        if(b[mid]>=x) r=mid;
        else l=mid+1;
    }

    while(b[l]==x&&l<t)
    {
        cnt++;
        l++;
    }
    return cnt;
}
int main()
{
    int n,i,j,T;
    long long res;
    scanf("%d",&T);
    while(T--)
    {

        scanf("%d",&n);
        res=0;
        for(i=0;i<n;i++)
            for(j=0;j<4;j++)
                scanf("%d",&p[i][j]);
        t=0;
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                a[t++]=p[i][0]+p[j][1];
        sort(a,a+t);
        t=0;
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                b[t++]=p[i][2]+p[j][3];
        sort(b,b+t);
        for(i=0;i<t;i++)
            res+=erfen(-a[i]);
        printf("%d\n",res);
         if(T) printf("\n");
        }
    return 0;
}

---恢复内容结束---

时间: 2024-12-27 03:03:10

J 中途相遇法,求和的相关文章

codeforces 525 E Anya and Cubes 中途相遇法

codeforces 525 E Anya and Cubes 中途相遇法 题意: 给出n个数a1,a2,...,an,要求从中选出一些数,可以把其中最多k个变成它自己的阶乘,然后选出的数求和,问最后和等于s的选法有多少种. 限制: 1 <= n <= 25; 0 <= k <= n; 1<= s <= 1e16; 1 <= ai <= 1e9 思路: 一般数据量20~30都会考虑中途相遇法,就是折半暴力. ps:用三进制暴力会比直接深搜多一个常数10,因为

uva 6757 Cup of Cowards(中途相遇法,貌似)

uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 di?erent characters (Mage, Tank, Fighter,Assassin and Marksman). A team consists of 5 players (one from each kind) and the goal is to kill amonster with L life points. The

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

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 62    Accepted Submission(s): 19 Problem Description Little Ruins is playing a number game, first he chooses two positive integers y an

【中途相遇法】【STL】BAPC2014 K Key to Knowledge

题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11674&courseid=0 题目大意: N个学生M道题(1<=N<=12,1<=M<=30),每道题只有正误两种选项(0 1),每个学生的答题情况和正确题数已知,求标准答案可能有多少种. 如果标准答案只有一种则输出标准答案,否则输出解的个数. 题目思路: [

Codeforces 31E TV Game 中途相遇法 状压dp

题目链接:点击打开链接 题意: 给定2*n长的数字. 把这个数字拆成2个长度为n的数字,且相对位置不变.使得拆后得到的2个数字的和最大. 输出一个方案. 显然是中途相遇法,先计算左半段,再计算右半段 分别状压左半段和右半段,注意左半段状压后要在末尾补上0. 代码估计哪里有小越界==,数组开大了一点才过..具体就不查了. #include<iostream> #include<stdio.h> #include<string.h> #include<string&g

UVA 1326 Jurassic Remains 中途相遇法

题目链接:点击打开链接 题意:给定n个字符串,选尽可能多的字符串使得每种字母出现的次数为偶数次 思路: 中途相遇法 import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedLi

POJ 1840 Eqs Hash + 中途相遇法

把等式分成两拨算用中途相遇法就好了. 不过要注意的是这里不能用map,会超时,要自己手写hash,我重载了[]操作符之后用起来和map差不多,很随意 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <qu

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib