传送门: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. 如何在Linux中tomcat下运行一个web项目

    如何在Linux中tomcat下运行一个web项目 然后启动Tomcat项目.运行的运行后会自动将war包解压. 如果页面报404,那么请查看tomcat日志文件,它一定是报错了....

  2. Const的使用

    const意味为readonly,即只读,const可被施加于任何作用域内的对象,函数参数,函数返回类型,成员函数本体 使用: const修饰变量时本质是 const在谁后面谁就不可修改,const在 ...

  3. CP-ABE ToolKit 安装笔记(转载)

    博主论文狗,好久没有来贴博客,最近做实验需要用到属性加密,了解了下CP-ABE,前来记录一下: 网上相关的博文较多,博主看了大部分的,认为下面这两个看完了基本就可以成功安装. 可参见博文: http: ...

  4. adb(Android Debug Bridge)安装使用教程

    一.说明 adb的db是debug bridge而不是和gdb一样指debug,这意思是说adb不能像gdb那样能一步步调试代码,但可以启到一些类似调试的功能. 下面就针对这些功能进行介绍,本文根据官 ...

  5. 关于TCP长连接和发送心跳的一些理解

    原因 TCP是一种有连接的协议,但是这个连接并不是指有一条实际的电路,而是一种虚拟的电路.TCP的建立连接和断开连接都是通过发送数据实现的,也就是我们常说的三次握手.四次挥手.TCP两端保存了一种数据 ...

  6. Python线程二

    转自:https://www.cnblogs.com/chengd/articles/7770898.html 1. threading.Lock() import threading balance ...

  7. 使用Redis数据库(1)(三十三)

    Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, So ...

  8. 从概率图模型pgm到rbm

    有向图模型:directed acyclic graph  DAG  贝叶斯网络 对称的,无向图, UGM: undirected graphic model  UGM, 更有名的名称是MRF,mar ...

  9. php 图片添加水印和二维码

    $host = $_SERVER['HTTP_HOST']; $save_code_file = './qrcodes/qrcode.png'; QrCode::format()->backgr ...

  10. learning at command AT+CGSN

    AT command AT+CGSN [Purpose]        Learning how to get mobile module international Mobile Equipment ...