传送门: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. Wannafly挑战赛27-A/B

    链接:https://www.nowcoder.com/acm/contest/215/A来源:牛客网 题目描述 “White shores, and beyond. A far green coun ...

  2. 实战dataguard主从切换

    前言: 众所周知DataGuard一般的切换分成两种,一种是系统正常的情况下的切换这种方式为:switchover是无损切换,不会丢失数据:另外一种方式属于灾难情况下的切换,这种情况下一般主库已经启动 ...

  3. Consider defining a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' in your configuration

    Description: Parameter 0 of method redisTemplate in com.liaojie.cloud.auth.server.config.redis.Redis ...

  4. [CodeForces - 197A] A - Plate Game

    A - Plate Game You've got a rectangular table with length a and width b and the infinite number of p ...

  5. ActiveMQ topic 普通订阅和持久订阅

    直观的结果:当生产者向 topic 发送消息, 1. 若不存在持久订阅者和在线的普通订阅者,这个消息不会保存,当普通订阅者上线后,它是收不到消息的. 2. 若存在离线的持久订阅者,broker 会为该 ...

  6. windows mfc 程序,不同程序通信和互斥

    1. 共享内存(项目中使用过) 我转备份文章:http://www.cnblogs.com/swing07/p/8087686.html CreateFileMapping 或 OpenFileMap ...

  7. zhuan 常用图像数据集:标注、检索

      目录(?)[+]   1.搜狗实验室数据集: http://www.sogou.com/labs/dl/p.html 互联网图片库来自sogou图片搜索所索引的部分数据.其中收集了包括人物.动物. ...

  8. jquery自定义类的封装

    如何用jquery自定义一个类?(demo参考) /*简单使用*/ (function($){ //el操纵对象,option属性值 $.love = function(el,option){ var ...

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

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

  10. flask项目结构(一)mariadb

    简介: 本文主要是根据自己所学,创建一个flask项目,使用sqlalchemy,alembic,mariadb,bootstrap,APScheduler,selenium,request…………技 ...