高效算法——J 中途相遇法,求和
---恢复内容开始---
Description

The SUM problem can be formulated as follows: given four lists A, B, C, D<tex2html_verbatim_mark> of integer values, compute how many quadruplet (a, b, c, d ) 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 A, B, C<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
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<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 中途相遇法,求和的更多相关文章
- 【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) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
Difference Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【UVALive】2965 Jurassic Remains(中途相遇法)
题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...
- uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)
用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...
- LA 2965 中途相遇法
题目链接:https://vjudge.net/problem/UVALive-2965 题意: 有很多字符串(24),选出一些字符串,要求这些字符串的字母都是偶数次: 分析: 暴力2^24也很大了, ...
- 中途相遇法 解决 超大背包问题 pack
Description [题目描述] 蛤布斯有n个物品和一个大小为m的背包,每个物品有大小和价值,它希望你帮它求出背包里最多能放下多少价值的物品. [输入数据] 第一行两个整数n,m.接下来n行每行两 ...
随机推荐
- Ant配置
首先去官网下载一个ant的文件 http://ant.apache.org/bindownload.cgi
- Android开发手记(28) Handler和Looper
Android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道.平 ...
- 设置tomcat启动超时,不会自动停止
tomcat启动时如果1000ms没有起来,服务就会自动停止.设置位置如下
- 英语学习[ZZ]
本文作者三年间从四级勉强及格到高级口译笔试210,口试232.找工作面试时给其口试的老外考官听了一分钟就说你的英语不用考了.虽不敢说方法一定是最好的,但从现在开始随便谁不要再去找学习资料,每天花两个钟 ...
- OpenCV中Mat的详解
每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1039-1-1.html 目标 我 ...
- SGU 124.Broken line
时间限制:0.25s 空间限制:4M 题意: 给出n条线段和一个点,保证所有线段平行X轴或Y,并且闭合成一个多边形.判断这个点的位置是在多边形上,还是多边形内,还是多边形外. solution: 由于 ...
- 【POJ3580】【块状链表】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- 与 空格的区别
nbsp 是 Non-Breaking SPace的缩写,即“不被折断的空格”,当两个单词使用 连接时,这两个单词就不会被分隔为2行,如下面 <div id="div1" ...
- Oracle 面试宝典 - General Questions
转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...
- Ubuntu 12.04 下安装配置 JDK 7(tar)
第一步:下载jdk-7u45-linux-i586.tar.gz 到Orcale的JDK官网下载JDK7的tar包 第二步:解压安装 tar -zxvf ./jdk-7u45-linux-i586.t ...