高效算法——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行每行两 ...
随机推荐
- java 生成pdf报表
public void saveMapAddressInfo(String orderCode){ try{ List<Leads> leadses = leadsService.find ...
- mysql - 初探
1,查询所有数据库名称: show databases; 2,查询所有表: use database_name; show tables; 3,查询表中的所有字段: desc table_name;
- mysql locktables
SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, TIMESTAMPDIFF( ...
- EBS成本核算方法
业务背景 成本核算方法,对应EBS系统中的成本方法,有四种: 1.标准成本 2.平均成本 平均成本又分为永续平均成本,即 Average Cost 期间平均成本,按照期间(自然月)来计算的平均成本 F ...
- throw 导致 Error C2220, wraning C4702错误
今天在程序加了一个语句,发现报 Error C2220, Wraning C4702错误 查询Wraning C4702 ,[无法访问的代码] 由于为 Visual Studio .NET 2003 ...
- c++ undefined reference to mysqlinit
Solved g++ $(mysql_config --cflags) file.cpp -o filename $(mysql_config --libs)
- java之两个字符串的比较
compareTo() 的返回值是int, 它是先比较对应字符的大小(ASCII码顺序)1.如果字符串相等返回值02.如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值 ...
- Wdcp两日志的路径
Wdcp两日志的路径: /www/wdlinux/httpd-2.2.22/logs /www/wdlinux/nginx-1.0.15/logs
- PHP获取客户端和服务器端IP
客户端的ip变量: $_SERVER['REMOTE_ADDR'] :客户端IP,也有可能是代理IP $_SERVER['HTTP_CLIENT_IP']:代理端的IP,可能存在,也可能伪造 $_SE ...
- 实用的透明背景mark图标