4 Values whose Sum is 0 POJ 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个整数的四个数列A,B,C,D。要从每个数列中各取出一个数,使四个数的和为0.求出这样的组合的个数,当一个数列中有多个相同的数字时,把它们作为不同的数字看待。
分析:
从四个数列中选择的话总共有n4种情况,所以全部判断一遍不可行。不过将它们对半分成AB和CD再考虑,就可以快速解决了。从两个序列中选择的话只有n2种组合,所以可以进行枚举。先从A、B中选择a,b后,为了使总和为0,则需要从C,D中取出c+d=-a-b。因此先将从A,B中取数字的n^2种方法全部枚举出来,将这些和排好序,这样就可以运用二分搜索了。
有时候,问题的规模较大,无法枚举所有元素的组合,但能够枚举一半元素的组合。此时,将问题拆成两半后分别枚举,再合并它们的结果,这一方法往往非常有效。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[4009],b[4009],c[4009],d[4009];
int num[16000000];
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
num[i*n+j]=a[i]+b[j];
sort(num,num+n*n);
long long int ans=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int cd=-(c[i]+d[j]);
ans+=upper_bound(num,num+n*n,cd)-lower_bound(num,num+n*n,cd);
}
printf("%lld\n",ans);
return 0;
}
4 Values whose Sum is 0 POJ 2785 (折半枚举)的更多相关文章
- 4 Values whose Sum is 0 POJ - 2785
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 29243 Accep ...
- Divide and conquer:4 Values whose Sum is 0(POJ 2785)
找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...
- 4 Values whose Sum is 0 POJ - 2785(二分应用)
题意:输入一个数字n,代表有n行a,b,c,d,求a+b+c+d=0有多少组情况. 思路:先求出前两个数字的所有情况,装在一个数组里面,再去求后两个数字的时候二分查找第一个大于等于这个数的位置和第一个 ...
- poj 2785(折半枚举+二分搜索)
传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- POJ 2785:4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 18221 Accep ...
随机推荐
- Swift as as!和as?的区别
1.as的使用场合 1.从派生类转换为基类,向上转类型(upcasting) class Animal{} class Dog:Animal{} let cat = ANimal() let dog ...
- JAVA中快速构建BEAN的方法
首先,创建一个JAVA类,testBean.java. package com.beans; public class testBean { } 然后,添加私有成员字段. package com.be ...
- 【Mysql】- Mysql 8.0正式版新亮点
MySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 注意:从 MySQL 5.7 升级到 MySQL 8 ...
- Python字符串的简单操作
数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...
- [BZOJ4822] [CQOI2017] 老C的任务
题目链接 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=4822. 洛谷:https://www.luogu.org/problemnew/sho ...
- 【HUD-5790】Prefix (主席树+tire)
似乎是归队赛的最后一道题. 由于当时以为是公共字串所以没写555555,其实是求公共前缀. 做法是建立tire,把tire上的点编号看成是值,查询第l到第r个字符串的区间内不重复的值的个数.建立主席树 ...
- poj3207 Ikki's Story IV - Panda's Trick 2-sat问题
---题面--- 题意:给定一个圈,m条边(给定),边可以通过外面连,也可以通过里面连,问连完这m条边后,是否可以做到边两两不相交 题解: 将连里面和连外面分别当做一种决策(即每条边都是决策点), 如 ...
- HDU2089:不要62——题解
http://acm.hdu.edu.cn/showproblem.php?pid=2089 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管 ...
- Windows相关
PE WIndows 相关知识 windows 主机防护
- JS深度合并对象
/** * 如果target(也就是FirstOBJ[key])存在, * 且是对象的话再去调用deepObjectMerge, * 否则就是FirstOBJ[key]里面没这个对象,需要与Secon ...