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

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[];
int b[];
int p[][];
int t;
int erfen(int x)
{
int cnt=;
int l=,r=t-,mid;
while(r>l)
{
mid=(l+r)>>;
if(b[mid]>=x) r=mid;
else l=mid+;
} 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=;
for(i=;i<n;i++)
for(j=;j<;j++)
scanf("%d",&p[i][j]);
t=;
for(i=;i<n;i++)
for(j=;j<n;j++)
a[t++]=p[i][]+p[j][];
sort(a,a+t);
t=;
for(i=;i<n;i++)
for(j=;j<n;j++)
b[t++]=p[i][]+p[j][];
sort(b,b+t);
for(i=;i<t;i++)
res+=erfen(-a[i]);
printf("%d\n",res);
if(T) printf("\n");
}
return ;
}

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

高效算法——J 中途相遇法,求和的更多相关文章

  1. 【uva 1152】4 Values Whose Sum is Zero(算法效率--中途相遇法+Hash或STL库)

    题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...

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

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  3. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

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

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  6. 【UVALive】2965 Jurassic Remains(中途相遇法)

    题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...

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

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

  8. LA 2965 中途相遇法

    题目链接:https://vjudge.net/problem/UVALive-2965 题意: 有很多字符串(24),选出一些字符串,要求这些字符串的字母都是偶数次: 分析: 暴力2^24也很大了, ...

  9. 中途相遇法 解决 超大背包问题 pack

    Description [题目描述] 蛤布斯有n个物品和一个大小为m的背包,每个物品有大小和价值,它希望你帮它求出背包里最多能放下多少价值的物品. [输入数据] 第一行两个整数n,m.接下来n行每行两 ...

随机推荐

  1. root密码忘记了要怎么搞

    我是根据这几个教程做的,网址如下 http://www.pc6.com/infoview/Article_65979.html http://down.chinaz.com/server/201111 ...

  2. Bash常用快捷键

    快捷键 作用 Ctrl+A 把光标移动到命令行开头,如果我们输入的命令过长,想要把光标移动到命令行开头时使用 Ctrl+E 把光标移动到命令行结尾 Ctrl+C 强制终止当前的命令 Ctrl+L 清屏 ...

  3. 2017JAVA必读书籍

    1.深入理解Java虚拟机:JVM高级特性与最佳实践 2.Oracle查询优化改写技巧与案例 3.Effective Java 4.Spring3.x企业应用开发实战 5.Spring技术内幕:深入解 ...

  4. 【原】iOS 获取当前和 前后n天的日期

    原文:http://www.cnblogs.com/A--G/p/4759810.html 一.获取当前日期需要调用一个简单的方法:[NSDate date];//获取现在的日期 -(NSString ...

  5. Missing iOS Distribution signing identity问题解决

    问题描述 打包上传APPStore  Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Develop证书 ...

  6. 【HOJ1356】【Miller_rabin素性测试】Prime Judge

    Given a positive integer, your job is writing a program to determine whether it is a prime number or ...

  7. 【HDU2815】【拓展BSGS】Mod Tree

    Problem Description   The picture indicates a tree, every node has 2 children.  The depth of the nod ...

  8. html良好结构-之豆瓣风格

    良好HTML结构 1 结构层次 unit> hd+ unit-wrap>section>item2 语议化的结构 html5 html 语议 ck-box-unit ck-hd-wr ...

  9. 阿里云 centos vim 显示中文 乱码

    开始以为是vim 设置编码的问题 :网上搜  改 .vimrc    无效!!! 后转战  是不是系统里面没有中文字体 1.先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的 ...

  10. mysql数据类型——浮点和定点型

    mysql数据类型 字节 含义 float(m,d)           4字节  单精度浮点型,8位精度,m是十进制数字的总个数,d是小数点后面的数字个数 double(m,d)        8字 ...