高效算法——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行每行两 ...
随机推荐
- sql语句中like的使用
先看一道题: 写出一条sql语句,找出表B中 字段Value中不全是字母 数字 下划线的数据 初看这道题,我们想到可以用like去进行模糊匹配,找出想要的结果.但是有一个地方需要注意:如果想在SQL ...
- 部分A+B_1
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...
- iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls
我们的自己的应用里面经常涉及的要打开ppt doc,这样的功能,以前总以为iOS沙盒封闭化,不可能实现,后来终于解决了 使用 UIDocumentInteractionController 来解决这一 ...
- JDBC标准事物编程模式
事物简介: 事物是一种数据库中保证交易可靠的机制,JDBC支持数据库中事物的概念,默认情况下事物是默认提交的. 事物的特性: 1.事物必须是原子工作单元,对于其数据的修改,要么都执行,要么都不执行2. ...
- 初试ubuntu14.4问题集锦
接触Linux系统也好长时间了,但每次都是浅尝则止.前几天突发兴趣,想认真地学习一下这个名扬天下的稳定的操作系统.于是试着装了一下炒作的最凶的Ubuntu. 安装的Ubuntu系统版本为14.04. ...
- 【POJ2761】【区间第k大】Feed the dogs(吐槽)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- PowerDesigner 的7种建模文件
1. 概念数据模型 (CDM) 对数据和信息进行建模,利用实体-关系图(E-R图)的形式组织数据,检验数据设计的有效性和合理性. 2. 逻辑数据模型 (LDM) PowerDesigner 15 ...
- ios开发之代理&&协议(补充篇)
一.理解协议与代理 协议: 协议是一个方法签名的列表,在其中可以定义若干个方法.根据配置,遵守该协议的类会去实现这个协议中规定的若干个方法. 代理: 代理是一个概念,很难用一个名词去定义(如我们可以说 ...
- web版扫雷小游戏(一)
作为一名程序猿,平时的爱好也不多,说起游戏,我不太喜欢大型的网游,因为太耗时间,偶尔玩玩经典的单机小游戏,比如windows下自带的游戏扫雷(秀一下,高级下最高纪录110s). 现阶段正在致力于web ...
- Bootstrap_表单_表单控件状态
一.焦点状态 焦点状态是通过伪类“:focus”来实现.Bootstrap框架中表单控件的焦点状态删除了outline的默认样式,重新添加阴影效果. <form role="form& ...