集训第四周(高效算法设计)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 ...
随机推荐
- LeetCode.893-特殊相等字符串组(Groups of Special-Equivalent Strings)
这是悦乐书的第344次更新,第368篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第209题(顺位题号是893). You are given an array A of ...
- Luogu P1144 最短路计数 【最短路】 By cellur925
题目传送门 常规的最短路计数问题:注意有重边(重边不用理,看样例),自环(读入时过滤). 另外这个无向图没有权,其实可以直接bfs做,但考虑到以后带权的情况,按spfa走了. 水题被卡了三次(嘤嘤嘤 ...
- python之计数统计
前言: 计数统计,简单的说就是统计某一项出现的次数.实际应用中很多需求都需要用到这个模型,如检测样本中某一值出现的次数.日志分析某一消息出现的频率.分析文件中相同字符串出现的概率等等.以下是实现的不同 ...
- 内置函数isinstance和issubclass
1. isinstance(obj,class) 判断对象obj是不是由class生成的对象. class Foo: pass obj=Foo() print(isinstance(obj,Foo)) ...
- vim插件minibuf配置
1.去下载网站下载minibufexpl.vim文件放入到~/vim/plugins中,有的系统路径是~/.vim/plugins; 下载网址如下 https://www.vim.org/script ...
- 原生jsonp跨域
<script> // jsonp跨域原生写法 var script = document.createElement('script'); script.src = 'http://19 ...
- MIPS的寄存器、指令和寻址方式的分类
MIPS的32个寄存器 助记符 编号 作用 zero 0 恒为0 at 1 (assembly temporary)保留给汇编器使用 v0,v1 2-3 (values)子程序返回,即函数调用时的返回 ...
- 常见的HTTP相应状态码
200:请求被正常处理204:请求被受理但没有资源可以返回206:客户端只是请求资源的一部分,服务器只对请求的部分资源执行GET方法,相应报文中通过Content-Range指定范围的资源.301:永 ...
- Domain Adaptation论文笔记
领域自适应问题一般有两个域,一个是源域,一个是目标域,领域自适应可利用来自源域的带标签的数据(源域中有大量带标签的数据)来帮助学习目标域中的网络参数(目标域中很少甚至没有带标签的数据).领域自适应如今 ...
- org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [********] will not be managed by Spring
如下图,查看层次是否正确.