集训第四周(高效算法设计)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 ...
随机推荐
- 如何验证自己的网络是否支持ipv6
http://test-ipv6.com/进入得到自己的ipv6地址 然后命令行 ping 一下
- Spring的事务传播性与隔离级别以及实现事物回滚
一.事务的四个特性(ACID) 原子性(Atomicity):一个事务中所有对数据库的操作是一个不可分割的操作序列,要么全做,要么全部做. 一致性(Consistency): 数据不会因为事务的执行而 ...
- Pycharm安装及第一次使用导航
Pycharm:Pycharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元 ...
- B - Crossword solving
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he ...
- C# 判断文件和文件夹是否存在并创建
C# 判断文件和文件夹是否存在并创建 using System; using System.Data; using System.Configuration; using System.Collect ...
- WebForm 开发方式,简单使用
ASP开发方式 格式 <% %> C#代码可以写在里面 <%= %> 往外输出一个值,可以放一个变量,一个方法(这个方法是有返回值的直接打印到界面上去) <%@ %& ...
- Android开发中使用数据库时出现java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
最近在开发一个 App 的时候用到了数据库,可是在使用数据库的时候就出现了一些问题,在我查询表中的一些信息时出现了一下问题: Caused by: java.lang.IllegalStateExce ...
- Python学习 Day 8 继承 多态 Type isinstance dir __slots__
继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base clas ...
- contact用法解析
经典用法: mysql> select concat('11','22','33'); +------------------------+ | concat('11','22','33') | ...
- 快速开发框架天梭(Tissot)
天梭(Tissot)集成SpringBoot+Dubbo等主流互联网技术栈,高度集成.优化方便快速搭建应用.某互金科技公司内部孵化框架,已应用于公司90%业务系统. 框架划分模块有: tissot-c ...