集训第四周(高效算法设计)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
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).
如果按照原始方法,进行四重循环,毫无疑问是会超时的,只能使用方法把四重循环变成两重,可是每一个数最大可达2的28次方,使用short开数组也是行不通的,做一个大整数的hash。。不会,只能使用数组保存值,之后再进行查找了。。。
#include"iostream"
#include"cstring"
#include"algorithm"
#include"map"
#include"cmath"
using namespace std;
const int maxn=4000+10;
int book[16000010];
int a[maxn],b[maxn],c[maxn],d[maxn];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
memset(book,0,sizeof(book));
for(int i=0;i<n;i++)
cin>>a[i]>>b[i]>>c[i]>>d[i]; int f=0;
for(int ia=0;ia<n;ia++)
for(int ib=0;ib<n;ib++)
{
book[f++]=-(a[ia]+b[ib]);
}
int sum=0;
sort(book,book+f);
for(int ic=0;ic<n;ic++)
for(int id=0;id<n;id++)
{
int temp=c[ic]+d[id];
int x1=lower_bound(book,book+f,temp)-book;
int x2=upper_bound(book,book+f,temp)-book;
sum+=x2-x1;
}
cout<<sum<<endl;
if(T) cout<<endl;
}
return 0;
}
集训第四周(高效算法设计)J题 (中途相遇法)的更多相关文章
- 高效算法——J 中途相遇法,求和
---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- 【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) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- 集训第四周(高效算法设计)I题 (贪心)
Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
- 集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...
- 集训第四周(高效算法设计)P题 (构造题)
Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
随机推荐
- mysql 循环批量插入
背景 前几天在MySql上做分页时,看到有博文说使用 limit 0,10 方式分页会有丢数据问题,有人又说不会,于是想自己测试一下.测试时没有数据,便安装了一个MySql,建了张表,在建了个whil ...
- centos 6.4 源码安装php5.4 mysql5.5 apahce2
centos 6.4 源码安装php5.4 mysql5.5 apahce2 博客分类: php 参考:http://blog.csdn.net/simpleiseasy/article/deta ...
- _bzoj1009 [HNOI2008]GT考试【矩阵加速dp】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1009 比较不错的一道题,令f(i, j)表示考号匹配到i位,后j位为不吉利串的前j位,那么对 ...
- python优缺点分析及python种类
Python的缺点: 相较于其它类型的语言可能运行速度上会略差.C语言的运行性能速度上最好,因为C最接近计算机底层. Python的优点: 大数据处理,有专门的功能模块,比较方便. Linux自带Py ...
- 升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)
package Day28ketangzuoye; import java.util.ArrayList; import java.util.Collections; import java.util ...
- Windows Phone8.1应用 提交
昨天晚上,试了试把一个WP8.1应用提交到微软商店上去,期间遇到了不少问题,搞到凌晨还没弄好.今天早上,终于把WP8.1应用成功提交上去了. 下面说说如何把WP8.1应用提交商店去.提交WP8.1以及 ...
- CCF|打酱油|Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = ...
- 使用 ArrayAdapter 来定制 ListView
一个 ListView,其宽高都设为 match_parent,可以更省资源. activity_main.xml <ListView android:id="@+id/list_Vi ...
- RGB、YUV和YCbCr介绍【转】
RGB: 就是常说的红(Red).绿(Green)和蓝(Blue),每个图像的像素点由RGB三个通道的值组成. YUV和YCbCr: YUV与RGB的转换: Y'= 0.299*R' + 0.587* ...
- 最新WIN10系统32位和64位纯净版自动激活版1010074 V2015年
系统来自:系统妈 本系统定位于个人在家庭.网吧.办公环境使用,采用久经考验的精简方法和体积压缩技术,在小巧体积中提供了几乎100%的原版Win10兼容性.经过在多个版本的更新和升级过程后,已经被证明能 ...