4 Values whose Sum is 0

Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 21370   Accepted: 6428
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).

Source

Southwestern Europe 2005

// 题意: 比如例子 6 行,每行 4 个数,从第一,二,三,四列各选一个数,要使和为 0 ,有几种组合?

n 最大有4000,但还是可以暴力,加二分

a + b = -( c + d )

枚举 a + b ,对于 c + d ,枚举一次,用一个大数组保存和,sort ,就可以二分了。要注意的是,二分到了要对左右继续搜索一下,不同的位置代表有不同的组合

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; int n;
int k; int num[][];
int s[]; int erfen(int t)
{
int l=,r=k-;
while (l<=r)
{
int mid=(l+r)/;
if (s[mid]==t)
{
int e = mid-;
int all=;
while (e>=&&s[e]==t)
e--,all++;
e=mid+;
while (e<k&&s[e]==t)
e++,all++;
return all;
}
else if (s[mid]>t)
r=mid-;
else
l=mid+;
}
return ;
} int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<n;i++)
scanf("%d%d%d%d",&num[i][],&num[i][],&num[i][],&num[i][]);
k=;
for (int i=;i<n;i++)
for (int j=;j<n;j++)
s[k++]=-(num[i][]+num[j][]);
sort(s,s+k);
int total=;
for (int i=;i<n;i++)
{
for (int j=;j<n;j++)
{
int left=num[i][]+num[j][];
total+=erfen(left);
}
}
printf("%d\n",total);
}
return ;
}

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: 25615   Accep ...

  2. POJ 2785:4 Values whose Sum is 0 二分

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

  3. POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找

    2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...

  4. UVA - 1152 --- 4 Values whose Sum is 0(二分)

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  5. POJ2785 4 Values whose Sum is 0 (二分)

    题意:给你四组长度为\(n\)序列,从每个序列中选一个数出来,使得四个数字之和等于\(0\),问由多少种组成情况(仅于元素的所在位置有关). 题解:\(n\)最大可以取4000,直接暴力肯定是不行的, ...

  6. [poj2785]4 Values whose Sum is 0(hash或二分)

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

  7. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

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

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

  9. 二分-G - 4 Values whose Sum is 0

    G - 4 Values whose Sum is 0 The SUM problem can be formulated as follows: given four lists A, B, C, ...

随机推荐

  1. nginx的301与302如何配置

    转自:http://blog.sina.com.cn/s/blog_5d73ba76010145rr.html 首先看一个完整代码示例,关于nginx 301 302跳转的. 301跳转设置: ser ...

  2. 资源的GPUAddress

    BufferAddress CommandHandle TextureHandle 给shader采样的 ImageHandle 给shader  load store的.../imageLoad() ...

  3. 2017.8.9在虚拟机中安装linux系统

    0 安装前提 已经安装好VM,并且配置好,具体过程参看随笔:2017.8.5 VMware的介绍与安装 相关随笔:2017.8.5 Linux达人养成计划 I 需要知道的概念:VMWare,虚拟机,真 ...

  4. Android Tabhost置于底部

    方法一: <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id=&qu ...

  5. ionic准备之angular基础——格式化数据以及过滤器(8)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. vue-router push

    //push 方法一 对象 // this.$router.push({path:'HelloWorld2'}); //push 方法二 命名的路由 // this.$router.push({nam ...

  7. 苹果版小黄车(ofo)app主页菜单效果

    代码地址如下:http://www.demodashi.com/demo/12823.html 前言: 最近又是公司项目上线一段时间了,又是到了程序汪整理代码的节奏了.刚好也用到了ofo主页菜单的效果 ...

  8. statusbar的颜色设置

    步骤一.在info.plist文件中添加一行(key:View controller-based status bar appearance,value:NO) 步骤二.在appDelegate中,添 ...

  9. C#中静态方法和非静态方法的区别

    静态方法和非静态方法的区别: 1.静态方法不需要类实例化就可以调用,反之非静态方法需要实例化后才能调用: 2.静态方法只能访问静态成员和方法,非静态方法都可以访问: 3.静态方法不能标记为overri ...

  10. ASP.NET CORE RAZOR :向 Razor 页面添加验证

    https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/validation 本部分中向 Movie 模型添加了验证逻辑. ...