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

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

  1. # include <cstdio>
  2. # include <cstring>
  3. # include <cstdlib>
  4. # include <iostream>
  5. # include <vector>
  6. # include <queue>
  7. # include <stack>
  8. # include <map>
  9. # include <set>
  10. # include <cmath>
  11. # include <algorithm>
  12. using namespace std;
  13. # define lowbit(x) ((x)&(-x))
  14. # define pi acos(-1.0)
  15. # define eps 1e-
  16. # define MOD
  17. # define INF
  18. # define mem(a,b) memset(a,b,sizeof(a))
  19. # define FOR(i,a,n) for(int i=a; i<=n; ++i)
  20. # define FO(i,a,n) for(int i=a; i<n; ++i)
  21. # define bug puts("H");
  22. # define lch p<<,l,mid
  23. # define rch p<<|,mid+,r
  24. # define mp make_pair
  25. # define pb push_back
  26. typedef pair<int,int> PII;
  27. typedef vector<int> VI;
  28. # pragma comment(linker, "/STACK:1024000000,1024000000")
  29. typedef long long LL;
  30. int Scan() {
  31. int res=, flag=;
  32. char ch;
  33. if((ch=getchar())=='-') flag=;
  34. else if(ch>=''&&ch<='') res=ch-'';
  35. while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
  36. return flag?-res:res;
  37. }
  38. void Out(int a) {
  39. if(a<) {putchar('-'); a=-a;}
  40. if(a>=) Out(a/);
  41. putchar(a%+'');
  42. }
  43. const int N=;
  44. //Code begin...
  45.  
  46. int a[N], b[N], c[N], d[N];
  47. VI vis;
  48.  
  49. int main ()
  50. {
  51. int n;
  52. LL ans=;
  53. n=Scan();
  54. FOR(i,,n) a[i]=Scan(), b[i]=Scan(), c[i]=Scan(), d[i]=Scan();
  55. FOR(i,,n) FOR(j,,n) vis.pb(-c[i]-d[j]);
  56. sort(vis.begin(),vis.end());
  57. FOR(i,,n) FOR(j,,n) {
  58. int temp=a[i]+b[j];
  59. ans+=upper_bound(vis.begin(),vis.end(),temp)-lower_bound(vis.begin(),vis.end(),temp);
  60. }
  61. printf("%lld\n",ans);
  62. return ;
  63. }

枚举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. 成都Uber优步司机奖励政策(1月25日)

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

  2. [hdu 6184 Counting Stars(三元环计数)

    hdu 6184 Counting Stars(三元环计数) 题意: 给一张n个点m条边的无向图,问有多少个\(A-structure\) 其中\(A-structure\)满足\(V=(A,B,C, ...

  3. 使用AutoFac实现依赖注入(封装一个注册类)

    public class AutoFacBootStrapper { public static void CoreAutoFacInit() { var builder = new Containe ...

  4. nio之netty5应用

    1.netty5和netty4的区别不是很大,但是与netty3差别还是有的.这里不介绍netty4,因为和netty5的方式都差不多.netty5的复杂性相对于netty3要多很多了.基本上架构都被 ...

  5. 前端图片转base64,转格式,转blob,上传的总结

    1. 图片文件转base64 <input accept="image/gif,image/jpeg,image/jpg,image/png" type="file ...

  6. Python中安装Prophet

    1. 先安装pystan依赖 按照https://pystan.readthedocs.io/en/latest/windows.html说明,请使用如下命令 conda install libpyt ...

  7. OSG-交互

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  8. CodeForces - 776C(前缀和+思维)

    链接:CodeForces - 776C 题意:给出数组 a[n] ,问有多少个区间和等于 k^x(x >= 0). 题解:求前缀和,标记每个和的个数.对每一个数都遍历到1e5,记录到答案. # ...

  9. leetcode-三数之和(java)

     三数之和     给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可 ...

  10. vector的基础使用

    vector是一个容器,实现动态数组. 相似点:下标从0开始. 不同点:vector创建对象后,容器大小会随着元素的增多或减少而变化. 基础操作: 1.使用vector需要添加头文件,#include ...