4 Values whose Sum is 0
Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 19243   Accepted: 5744
Case Time Limit: 5000MS

Description

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

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

Hint

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).
 
题目大意:四个数列,问你有几种可能的组合满足,a[i]+b[j]+c[k]+d[e]==0;
思路分析:直接遍历所有情况的话,复杂度是n^4。肯定超时,本题应该采用二分做,先进行数组合并,将四个数组合成为两个n*n的数组,然后对
其中一个排序,这样就可以用二分查找另一个数组是否存在对应的数,这样复杂度就变成了n*nlogn^2,这样就不会超时了。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int inf=0xffffff;
const int maxn=4000+10;
int a[maxn],b[maxn],c[maxn],d[maxn];
int f1[maxn*maxn],f2[maxn*maxn];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int cut=0;
        for(int i=0;i<n;i++)
            scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                f1[i*n+j]=a[i]+b[j];
                f2[i*n+j]=c[i]+d[j];
            }
        }
        sort(f2,f2+n*n);
    for(int i=0;i<n*n;i++)
    {
        int low=0,high=n*n-1;
    while(low<=high)
    {
        int mid=(low+high)/2;
        if(f2[mid]==-f1[i])
        {
             cut++;
             for(int j=mid-1;j>=0;j--)
             {
                 if(f2[j]!=-f1[i]) break;
                 cut++;
             }
             for(int j=mid+1;j<n*n;j++)
             {
                  if(f2[j]!=-f1[i]) break;
                 cut++;
             }
             break;
        }
      else if(f2[mid]<-f1[i]) low=mid+1;
      else high=mid-1;
    }
    }
    cout<<cut<<endl;
    }
    return 0;
}

poj2785 简单二分的更多相关文章

  1. HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)

    二分是一种很有效的减少时间开销的策略, 我觉得单列出二分专题有些不太合理, 二分应该作为一中优化方法来考虑 这几道题都是简单的使用了二分方法优化, 二分虽然看似很简单, 但一不注意就会犯错. 在写二分 ...

  2. 一些简单二分题,简单的hash,H(i),字符串题

    说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...

  3. hdu-4185.loiol_skimming(简单二分匹配模型)

    /************************************************************************* > File Name: hdu-4185. ...

  4. POJ2239简单二分匹配

    题意:       一周有7天,每天可以上12节课,现在给你每科课的上课时间,问你一周最多可以上几科课,一科课只要上一节就行了. 思路:       简单题目,直接二分就行了,好久没写二分匹配了,练习 ...

  5. CF 706B 简单二分,水

    1.CF 706B  Interesting drink 2.链接:http://codeforces.com/problemset/problem/706/B 3.总结:二分 题意:给出n个数,再给 ...

  6. poj 3273 Monthly Expence 简单二分

    /** 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大 ...

  7. Codeforces 846D Monitor(简单二分+二维BIT)

    D. Monitor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  8. SPOJ-COLONY - Linearian Colony!简单二分思想

    COLONY - Linearian Colony 一道很水的题却坑我两天!在CF上做过类似的题,用递归可以找到答案,但感觉不会这么麻烦,于是看看有没有什么规律,对Y分奇偶貌似可以找到规律,但WA了三 ...

  9. 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题

    如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...

随机推荐

  1. C++拾遗(一)关于main()函数

    C:省略返回值默认为int,()中空着不等于void C++:不能省略返回值,()中空着等效于(void) 常规独立程序必须包含一个main(),DLL可以不需要main().

  2. SQL server 2008无法修改表

    长久未使用SQL server,一直都是使用Navicat来处理各种数据库,感觉使用很方便,但由于实际需要,必须要用SQL server创建新的数据库,却意外的遇到了以前从未遇到过的问题. 在建好表以 ...

  3. java学习笔记 (7) —— 实现简单的上传处理

    1.下载apache 的 commons-fileupload.jar 包及 commons-io.jar 2.引入到lib 文件夹下 3.新建uploadApache.jsp <%@ page ...

  4. JQuery获取当前屏幕的高度宽度

    JQuery获取浏览器窗口宽高,文档宽高的代码,使用jquery的朋友可以参考下. <script type="text/javascript"> $(document ...

  5. org/hamcrest/SelfDescribing

    今天在Idea下使用JUtil的时候,运行@Test,报错:org/hamcrest/SelfDescribing 解决办法: (1)换成junit-4.8.jar (2)junit-4.11.jar ...

  6. Ubuntu 14.04 标题栏实时显示上下行网速、CPU及内存使用情况

    首先当然是用wget下载indicator-sysmonitor,终端执行命令:wget -c https://launchpad.net/indicator-sysmonitor/trunk/4.0 ...

  7. (摘)oracle dataguard切换

    众所周知,Data Guard已经是现今标准的主流容灾方案,由于日志传递对于网络适应程度强,且可以采用同步实时的传递方式和异步延迟的传递方式,甚至可以成为远程的异地容灾方案.不管用于何种用途,DG都免 ...

  8. 通过内省机制设置JavaBean

    一.步骤: 1)使用PropertyDescriptor类获取属性描述者对象 //pd引用Student的name属性 PropertyDescriptor pd = new PropertyDesc ...

  9. simulate windows postmessage or keydown

    2 ways: 1. under TForm:   if assigned(focused) then      Focused.keydown(key,keychar,[]); 2. using s ...

  10. translate函数说明

    TRANSLATE(expr, from_string, to_string) from_string 与 to_string 以字符为单位,对应字符一一替换. SQL> SELECT TRAN ...