传送门: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. php获得时间段的月

    1.时间:$start_time = $_GET['start_time']; //2015-01$end_time = $_GET['end_time']; //2015-052.对时间进行拆分:$ ...

  2. leetcode-algorithms-25 Reverse Nodes in k-Group

    leetcode-algorithms-25 Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked l ...

  3. js操作字符串的常用方法

    使用 substring()或者slice() 函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=the ...

  4. 牛客网第一场 A Monotonic Matrix

    链接:https://www.nowcoder.com/acm/contest/139/A来源:牛客网 Count the number of n x m matrices A satisfying ...

  5. QPainter、QPainterPath、QBrush

    参考资料: https://blog.csdn.net/qq_35488967/article/details/70802973https://blog.csdn.net/wanghualin033/ ...

  6. html盒子水平和垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. MAVEN ECLIPSE JAR工程

    在eclipse 空白处点击鼠标右键选择新建 project 选择maven project: 选择Create a simple project Group ID: Artifact ID:创建项目 ...

  8. add()方法和Put()方法的差别

    add()和put()方法都是集合框架中的添加元素的方法. 但是put()方法应用于map集合中,add()方法应用于collection集合中. 二者的主要区别是:返回值类型不一样. add()放回 ...

  9. 程序从sqlserver2008搬家到MySQL5.6

    1.数据表结构的搬家 SqlServer的建表sql语句在MySQL中肯定不能运行 这里使用转换工具 下载地址: https://download.csdn.net/download/zhutouai ...

  10. 数据库编程测试机试 QQ

    创建QQ数据库 #创建数据库 CREATE DATABASE QQ #创建表名 并且添加列 DROP TABLE IF EXISTS `dbo.BaseInfo`; CREATE TABLE `stu ...