给出四个长度为n的数列a,b,c,d,求从这四个数列中每个选取一个元素后的和为0的方法数。n<=4000,abs(val)<=2^28.

考虑直接暴力,复杂度O(n^4).显然超时。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int a[N], b[N], c[N], d[N];
VI vis; int main ()
{
int n;
LL ans=;
n=Scan();
FOR(i,,n) a[i]=Scan(), b[i]=Scan(), c[i]=Scan(), d[i]=Scan();
FOR(i,,n) FOR(j,,n) vis.pb(-c[i]-d[j]);
sort(vis.begin(),vis.end());
FOR(i,,n) FOR(j,,n) {
int temp=a[i]+b[j];
ans+=upper_bound(vis.begin(),vis.end(),temp)-lower_bound(vis.begin(),vis.end(),temp);
}
printf("%lld\n",ans);
return ;
}

枚举a,二分b+c+d.复杂度O(n+n^3*log(n^3)+n*log(n^3))~O(n^3*logn).

枚举a+b,二分b+c.复杂度O(n^2+n^2*log(n^2)+n^2*log(n^2))~O(n^2*logn).

枚举a+b+c,二分d.复杂度O(n^3+logn+n^3*logn)~O(n^3*logn).

另外此题map常数大过不了。

POJ 2785 4 Values whose Sum is 0(折半枚举)的更多相关文章

  1. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  2. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  3. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  4. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

  5. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  6. POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)

    题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...

  7. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  8. POJ 2785 4 Values whose Sum is 0 Hash!

    http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

  9. 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 ...

随机推荐

  1. 毕业 迷茫 继续OR放弃?

    终于还是回学校拿了毕业证书,请了两个小时的假,连同中午吃饭的时间,顺利地从班主任手中拿到了毕业证书,大学就这样结束啦,人生也来到了一个新的转折点,但是每天的每天,我都是异常迷茫,异常胆怯,异常无语. ...

  2. 北京优步UBER司机B组最新奖励政策、高峰翻倍奖励、行程奖励、金牌司机奖励【每周更新】

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 成都Uber优步司机奖励政策(1月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. SQl 语句 表的连接

    当涉及到多个表查询时就需要使用将表连接的方法进行查询. SQL语句连接的方式根本上分为5种: •EQUI JOIN •SEMI JOIN 3 •ANTI JOIN 4 •CROSS JOIN •DIV ...

  5. ReadyAPI 教程和示例(二)

    声明:如果你想转载,请标明本篇博客的链接,请多多尊重原创,谢谢! 本篇使用的 ReadyAPI版本是2.5.0 接上一篇: 4.修改SoapUI测试 本节将演示如何为测试用例添加测试步骤以及更改请求参 ...

  6. 在deepin系统中制作桌面快捷方式

    在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...

  7. mahout协同过滤算法各接口

    Mahout协同过滤算法 Mahout使用了Taste来提高协同过滤算法的实现,它是一个基于Java实现的可扩展的,高效的推荐引擎.Taste既实现了最基本的基于用户的和基于内容的推荐算法,同时也提供 ...

  8. 动画效果 ObjectAnimator

    学习了一下动画效果的使用,做一下笔记 ImageView imageView = findViewById(R.id.imageView); ObjectAnimator.ofFloat(imageV ...

  9. Tensorflow学习第1课——从本地加载MNIST以及FashionMNIST数据

    很多Tensorflow第一课的教程都是使用MNIST或者FashionMNIST数据集作为示例数据集,但是其给的例程基本都是从网络上用load_data函数直接加载,该函数封装程度比较高,如果网络出 ...

  10. day-17 L1和L2正则化的tensorflow示例

    机器学习中几乎都可以看到损失函数后面会添加一个额外项,常用的额外项一般有两种,一般英文称作ℓ1-norm和ℓ2-norm,中文称作L1正则化和L2正则化,或者L1范数和L2范数.L2范数也被称为权重衰 ...