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: ...
随机推荐
- selenium技术博客
1.java+selenium+详细的api说明和seleniumGrid使用,缺点是代码不够好看. http://www.cnblogs.com/yytesting/p/5714175.html
- Matlab中hold on与hold off的用法
摘录自:https://blog.csdn.net/smf0504/article/details/51830963 https://www.cnblogs.com/shuqingstudy/p/48 ...
- 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案
一. 简介 揭秘: SimpleThreadPool是Quartz.Net中自带的线程池,默认个数为10个,代表一个Scheduler同一时刻并发的最多只能执行10个job,超过10个的job需要排队 ...
- 第十节:基于MVC5+Unity+EF+Log4Net的基础结构搭建
一. 前言 本节继续探讨一种新的框架搭建模式,框架的结构划分和上一节是相同的,本节IOC框架换成了Unity,并且采用构造函数注入的方式,另外服务层的封装模式也发生了变化,下面将详细的进行探讨. (一 ...
- 数字化IT人才与组织
企业的数字化目标 数字时代需要怎样的IT 数字化平台战略 产品一体化交付能力 产品设计与规划的能力 路线图 数字人才框架 Tips: 赋能授权(Empowerment)是近年来应最多的商业语汇之一.赋 ...
- FTP主动及被动模式效果图
- 工作经验-类型转换[ java.lang.String]
错误代码: logger.info("1"); Map<String,String> zxmap = (Map) zxList.get(0); ybty.setFyze ...
- JS禁用浏览器退格键、禁止右键、禁止全选、复制、粘贴
一.禁用浏览器退格键 摘抄自:https://www.cnblogs.com/wanggd/p/3164536.html 我们在真实的项目开发中经常会使用JS 对键盘上的一些按键进行禁用,常见的比如说 ...
- 【转】使用keepalived设置虚拟IP示例
准备 准备两台机器,IP地址信息如下: host1: 192.168.56.103 host2: 192.168.56.104 为了测试需要,分别在两台机器上安装apache并启动httpd服务,使下 ...
- 数位dp 的简单入门
时间紧张,就不讲那么详细了. 之前一直被深搜代码误解,以为数位dp 其实就是记忆化深搜...(虽说爆搜确实很舒服而且还好想) 但是后来发现数位dp 的标准格式其实是 预处理 + dp ...... 数 ...