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. JqueryUI插件网络连接

    operamasks_UI官网 http://ui.operamasks.org/website/homepage.html EasyUI官网 http://www.jeasyui.com/index ...

  2. JavaScript加密解密压缩工具

    <script> a=62; function encode() { var code = document.getElementById('code').value; code = co ...

  3. notepad++搜索结果不显示line XX的方法

    在使用notepad++如果多次搜索,得到的结果中会出现多次line xx: line xx:,造成文件大量垃圾信息的存在,不利于找寻所需的内容,如下图.                对于这种情况, ...

  4. C#文本文件导入数据库

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...

  5. 使用Azure云存储构建高速 Docker registry

    使用Azure云存储构建高速 Docker registry 使用Docker来构建应用程序最常见的操作就是 docker run 或者 docker pull了,但是由于众所周知的原因,在国内想要高 ...

  6. cpm效果介绍

    浮层菜单 图片弹窗

  7. android:layout_weight属性的简单使用

    效果: style.xml <style name="etStyle2"> <item name="android:layout_width" ...

  8. qt 拖拽 修改大小(使用了nativeEvent和winEvent)

    http://www.cnblogs.com/swarmbees/p/5621543.html http://blog.sina.com.cn/s/blog_9e59cf590102w3r6.html

  9. Linux企业级项目实践之网络爬虫(25)——管理源代码之SVN

    软件项目开发中,一般会用到源代码管理工具SVN.版本控制是管理数据变更的一种技术.对于程序员来说,它已经成为不可或缺的工具,因为他们经常修改软件代码,产生部分的变更,然后第二天再取消所有的变更.想象有 ...

  10. 【转】Win7与Ubuntu 14.04双系统修改启动项顺序

    原文网址:http://blog.sina.com.cn/s/blog_b381a98e0102v1gy.html 一.Ubuntu14.04采用默认安装方式的情况 采用默认安装方式,在win7下安装 ...