传送门:http://poj.org/problem?id=2785

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

给你一个数字n,然后有n组每组4个数,求每一列取出一个数,使最终四个数的和为0,求有多少种组合方式

解题思路:先求出来前两列和后两列的和,然后二分就可以了,对于这道题来说结果没有去重

 #include<vector>
#include<set>
#include<stdio.h>
#include<algorithm>
using namespace std; vector <int > a;
vector <int > b;
vector <int > c;
vector <int > d;
vector <int > sum1;
vector <int > sum2;
int main()
{
int n, i, j, a1, b1, c1, d1, sum;
while(scanf("%d", &n)!=EOF) {
sum = ;
for(i = ; i < n; i++)
{
scanf("%d%d%d%d", &a1, &b1, &c1, &d1);
a.push_back(a1);
b.push_back(b1);
c.push_back(c1);
d.push_back(d1);
}
for(i = ; i < n; i++) {
for(j = ; j < n; j++) {
sum1.push_back(a[i] + b[j]);
sum2.push_back(c[i] + d[j]);
}
} /* for(i = 0; i < sum1.size(); i++) {
for(j = 0; j < sum2.size(); j++) {
if(sum1[i]+sum2[j] == 0) {
sum++;
}
}
}*/
n = sum2.size();
sort(sum2.begin(),sum2.end()); for(i = ; i < n; i++) {
int l = , r = n-, mid;
while(l <= r) {
mid = (l + r) / ;
if(sum1[i] + sum2[mid] == ) {
sum++;
for(j = mid + ; j < n; j++) {
if(sum1[i] + sum2[j] != )
break;
else
sum++;
}
for(j = mid - ; j >= ; j--) {
if(sum1[i] + sum2[j] != )
break;
else
sum++;
}
break;
}
if(sum1[i] + sum2[mid] < )
l = mid + ;
else
r = mid - ;
}
}
printf("%d\n", sum);
}
return ;
}
 

POJ2785-4 Values whose Sum is 0的更多相关文章

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

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

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

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

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

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

  4. 折半枚举(双向搜索)poj27854 Values whose Sum is 0

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

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

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

  6. POJ 2785 4 Values whose Sum is 0

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

  7. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

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

  8. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

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

  10. UVA1152-4 Values whose Sum is 0(分块)

    Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time Limit: 9000 mSec Problem Desc ...

随机推荐

  1. Git将本地项目上传到GitHub

    本文转载于:https://segmentfault.com/a/1190000011909294 https://www.cnblogs.com/cxk1995/p/5800196.html 我们使 ...

  2. 什么是CSS hack?

    1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的,CSS hack就是让你记住这个标准),以 ...

  3. Django之用户认证功能

    前言 做web应用对登录做用户身份认证,然后设置session是必不可少的,因为我们就需要把有权限访问本站视图的用户,单独建一张表记录到数据库里: Django作为一个大而全的框架,已经为我们做好了这 ...

  4. time_wait 和 close_wait

    tcp 四次握手状态图: 使用以下命令统计 tcp 连接信息: netstat -n |awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}' ...

  5. Linux确认网口对应配置文件

    服务器经常是多网卡多网口,我们在某个网口插上网线后,到/etc/sysconfig/network-scripts/下配置ip时需要确定插上网线的网口对应的是哪个配置文件(比如是eth0还是eth1) ...

  6. pyqt实现滑动开关

    https://www.cnblogs.com/feiyangqingyun/p/6035633.html 根据Qt的实现,在PyQt上面实现了滑动开关的控件 import sys from PyQt ...

  7. pycuda安装 python<3.0

    cd pycudapython ./configure.py –cuda-root=/usr/local/cuda –cudadrv-lib-dir=/usr/lib –boost-inc-dir=/ ...

  8. ci框架memcached使用

    首先第一步需要安装memcached扩展 安装方法这里就不叙述了 application/config/memcached.php配置 $config = array( 'default' => ...

  9. 内联函数inline的用法

    一.什么是内联函数 在C语言中,如果一些函数被频繁调用,不断地有函数入栈,即函数栈,会造成栈空间或栈内存的大量消耗.为了解决这个问题,特别的引入了inline修饰符,表示为内联函数.  栈空间就是指放 ...

  10. os 模块 和 os模块下的path模块

    import os # os 主要用于与操作系统进行交互 #获取当前的工作目录 print(os.getcwd()) #切换工作目录 os .chdir("D:\上海python全栈4期\d ...