折半枚举(双向搜索)poj27854 Values whose Sum is 0
|
4 Values whose Sum is 0
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 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).
Source |
|||||||||
[Submit] [Go Back] [Status]
[Discuss]
有时候问题的规模比较大,无法枚举所有元素的组合,但能够枚举一般元素的组合。此时,将问题拆成两半后分别枚举,再合并他们的结果这一方法往往非常有效。
//折半枚举(双向搜索)poj2785
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=5005;
int n;
ll a[maxn],b[maxn],c[maxn],d[maxn];
ll cd[maxn*maxn];
void solve()
{
//枚举cd的组合
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cd[i*n+j]=c[i]+d[j];
}
}
sort(cd,cd+n*n);
ll res=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ll CD=-(a[i]+b[j]);
//二分搜索取出cd中和为CD的部分
res+=upper_bound(cd,cd+n*n,CD)-lower_bound(cd,cd+n*n,CD);
}
}
printf("%lld\n",res);
}
int main()
{
cin>>n;
for(int j=0;j<n;j++)
{
cin>>a[j]>>b[j]>>c[j]>>d[j];
}
solve();
return 0;
}
折半枚举(双向搜索)poj27854 Values whose Sum is 0的更多相关文章
- POJ2785-4 Values whose Sum is 0
传送门:http://poj.org/problem?id=2785 Description The SUM problem can be formulated as follows: given f ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- 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< ...
- POJ:2785-4 Values whose Sum is 0(双向搜索)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 26974 Accepted: ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- UVA1152-4 Values whose Sum is 0(分块)
Problem UVA1152-4 Values whose Sum is 0 Accept: 794 Submit: 10087Time Limit: 9000 mSec Problem Desc ...
- 4 Values whose Sum is 0(二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 21370 Accep ...
- 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 ...
随机推荐
- bzoj1052 [HAOI2007]覆盖问题 - 贪心
Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用3个L*L的正方形塑料薄膜将小 ...
- Django开发:(3.2)ORM:多表操作
表关系总结: 一对多:在多的表中建立关联字段 多对多:创建第三张表(关联表):id 和 两个关联字段 一对一:在两张表中的任意一张表中建立关联字段(关联字段一定要加 unique 约束) 子查询:一次 ...
- Combinations(带for循环的DFS)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Elasticsearch学习系列之多文档操作mget
测试数据 GET /library/books/1 { "_index": "library", "_type": "books& ...
- Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all kn ...
- 《编程导论(Java)·3.1.2 方法》之 副作用
4. 副作用 在一些语言如Pascal中,子程序被分成两种:函数和过程.尽管Java没有强制性地要求将方法区分为命令和函数.然而这样的差别对于良好地设计程序有非常大的帮助[1]. 首先说明一个概念:副 ...
- js美化压缩工具Mark一下
jscompress https://www.jscompress.cn/
- 程序中使用cocostudio移植到android手机须要的若干配置过程
首先在解决方式下加入现有项: libCocosStudio.vcxproj E$uVS5Sbv! WL:0n"BExtensions.vcxproj libGUI.vcxproj 然后在pr ...
- karaf增加自己定义log4j的配置
配置文件: karaf_home/etc/org.ops4j.pax.logging.cfg 增加配置: ### direct log messages to stdout ### log4j.app ...
- docker映射端口与ssh访问或容器访问
映射端口 -d 后台执行 -p映射端口 --privileged 可以使用systemctl # docker run --privileged -d -p 9000:80 jiqing9006/ce ...