4 Values whose Sum is 0 [POJ2785] [折半搜索]
题意
给你长度为n四个数列,每个数列选一个数使总和为4,有多少种选法(不同选法仅当起码有一个元素的下标不同)
输入
第一行,n
下面n行,每行四个数,代表ai,bi,ci,di
输出
选法数量
样例输入
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
分析
1、傻逼算法:四重枚举 O(n4)
2、优化一点的傻逼算法:三重枚举+一重二分O(n3logn)
3、比较优化算法:枚举c和d组成的数生成e,e排序,再枚举a,b,与e配对O(n2logn)
代码
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register ll
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
#define maxn 4005
using namespace std;
ll n,cnt,ans;
ll a[maxn],b[maxn],c[maxn],d[maxn],e[maxn*maxn];
inline ll read()
{
ll x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int main()
{
n=read();
rep(i,,n) a[i]=read(),b[i]=read(),c[i]=read(),d[i]=read();
rep(i,,n)rep(j,,n) e[++cnt]=c[i]+d[j];
sort(e+,e++cnt);
rep(i,,n)
rep(j,,n)
{
ll fd=-a[i]-b[j];
ans+=upper_bound(e+,e++cnt,fd)-lower_bound(e+,e++cnt,fd);
}
cout<<ans;
return ;
}
4 Values whose Sum is 0 [POJ2785] [折半搜索]的更多相关文章
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- Day3-J-4 Values whose Sum is 0 POJ2785
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...
- poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
Description The SUM problem can be formulated . In the following, we assume that all lists have the ...
- POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...
- 折半枚举(双向搜索)poj27854 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 23757 Accep ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...
随机推荐
- go 的匿名函数和闭包
匿名函数 匿名函数是指不需要定义函数名的一种函数实现方式. 在Go语言中,函数可以像普通变量一样被传递或使用,这与C语言的回调函数比较类似.不同的是,Go语言支持随时在代码里定义匿名函数. 匿名函数由 ...
- RSA Encrypting/Decrypting、RSA+AES Encrypting/Decrypting
catalogue . CryptoAPI介绍 . RSA Encrypting/Decrypting File 1. CryptoAPI介绍 0x1: Cryptography Service Pr ...
- Nginx常用的平滑重启
之前在做运维工作中,经常需要添加虚拟主机,或者添加修改配置文件,但是测试环境还好,随便玩,如果是生产环境的话,既要保证配置不出问题,有不能中断服务.如果是这样的话,就需要对配置文件进行语法检测以及平滑 ...
- win10添加右键打开命令窗口
新建文件cmd.reg,将下面代码贴入 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\she ...
- $\be$-QGE 的弱强唯一性
在 [Zhao, Jihong; Liu, Qiao. Weak-strong uniqueness criterion for the $\beta$-generalized surface qua ...
- RT-SA-2019-004 Cisco RV320 Unauthenticated Diagnostic DataRetrieval
Advisory: Cisco RV320 Unauthenticated Diagnostic Data Retrieval RedTeam Pentesting discovered that t ...
- word20161227客厅家电
1.Applicances 厨房.清洁用家电 2.Electronics 电视.音箱类家电 3.White goods 七八十年代家电基本都是白色的,所以流行叫白色家电,至今沿用 4.sleek an ...
- Distance on the tree(数剖 + 主席树)
题目链接:https://nanti.jisuanke.com/t/38229 题目大意:给你n个点,n-1条边,然后是m次询问,每一次询问给你u,v,w然后问你从u -> v 的路径上有多少边 ...
- 【全网最全的博客美化系列教程】08.自定义地址栏Logo
全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...
- Beta冲刺(5/7)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(5/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 ppt制作中 数据集标注 接下来的计划 制作p ...