poj 2785 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 = . 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 ). We then have n lines containing four integer values (with absolute value as large as ) 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
- -
- -
- -
- - -
- -
- - -
Sample Output
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-, -, , ), (, , -, -), (-, , , -),(-, , -, ), (-, -, , ).
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
#define ll long long
#define N 4006
int n;
int mp[N][N];
int A[N],B[N],C[N],D[N];
int CD[N*N];
void solve(){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
CD[i*n+j]=C[i]+D[j];
}
}
sort(CD,CD+n*n);
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
int cd = -(A[i]+B[j]);
ans+=upper_bound(CD,CD+n*n,cd)-lower_bound(CD,CD+n*n,cd);
}
}
printf("%I64d\n",ans);
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
for(int j=;j<;j++){
scanf("%d",&mp[i][j]);
}
}
for(int i=;i<n;i++){
A[i] = mp[i][];
B[i] = mp[i][];
C[i] = mp[i][];
D[i] = mp[i][];
}
solve();
}
return ;
}
poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))的更多相关文章
- POJ 2785 4 Values whose Sum is 0(折半枚举)
给出四个长度为n的数列a,b,c,d,求从这四个数列中每个选取一个元素后的和为0的方法数.n<=4000,abs(val)<=2^28. 考虑直接暴力,复杂度O(n^4).显然超时. # ...
- 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(折半枚举 sort + 二分)
题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...
- POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)
题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...
- POJ 2785 4 Values whose Sum is 0(哈希表)
[题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...
- POJ 2785 4 Values whose Sum is 0 Hash!
http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...
随机推荐
- 关于ionic的一些坑(1)
既然来了,总要留下点什么证明自己来过不是,今天就扒一扒自己在ionic上面遇到的坑,因为在项目中2还没出来,所以现在所遇到的都是1中的,关于2的,待老夫以后详细摸索之后在与君细细道来. 1.ionic ...
- ETL-Career RoadMap
RoadMap: 1.Tester:sql的单体或批处理测试: 2. Application Developer 2.1 批处理手动工具(如何使用.如何调度批处理.如何生成批处理脚本): 2.2 批处 ...
- ListView OnItemClickListener position 索引不正确
在使用ListView添加如下代码时 listview.setOnItemClickListener(new OnItemClickListener() { @Override public void ...
- hdu1869六度分离(dijkstra)
Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相 ...
- Windows下Node.js开发环境搭建-合适的开发环境
1)生产环境中的Node.js应用 Windows + Linus 2)虚拟机工具 VirtualBox 虚拟机CentOS安装 3)xShell与xFtp(windows到linux文件传输) 4) ...
- django: db - many to one
models 模块中的对象有三种对应关系:多对一,多对多,一对一.本讲说明多对一关系. blog/models.py: from django.db import models class Emplo ...
- F# 可以把几个函数组合成新函数
C#能做的,F#基本都能做,但F#能做的,C#未必能做. F#中的函数可以把几个函数组合起来使用.下面的例子是把由 function1 和 function2 这两个函数通过运算符“>>” ...
- 初学Java ssh之Spring 第二篇
上一篇我们成功搭建好了spring开发的环境,接下来一起看看spring有什么神奇的地方吧(本人也是新手,有不太对的地方希望大神给指出便于修改呢,谢谢大家). 之前听说spring是在对xml文件的应 ...
- iOS ui界面vtf 开发
addConstraints 添加约束的步奏 添加控件到view中 设置translateResizeLayoutintoautolayout = false 添加约束 注意 约束 : 出现 有父子关 ...
- 利用.htaccess绑定域名到子目录
前提首先得把域名绑定绑定了,比如把dev.ccvita.com解析到211.136.108.190这个IP其次是在网站管理面板里,为网站绑定dev.ccvita.com最后编辑配置.htaccess文 ...