POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
题目链接:http://poj.org/problem?id=2785
题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数.
其中一个数列有多个相同的数字时,把他们看作不同的数字.
做法是把前两个数列和的值存在一个数组(A)中 , 后两个数列的和存在另一个数组(B)中 , 数组都为n^2 . 然后将B数组sort一下 , 将A数组遍历 , 二分查找一下B数组中与A数组元素和为0的个数 . 有个注意的点是万一A数组都是0 , 而B数组都为0的情况(还有其他情况) , 那二分只能找到一个与之符合的情况 . 所以可以再找刚好比符合数大的数 , 相减他们的位置, 然后加起来 , 就是答案.
这里可以用到两个函数lower_bound()和upper_bound() ,都用了二分查找,前面的函数是返回一个数组中大于或等于一个数的位置,后面的是返回大于这个数的位置(不懂的可以Google or baidu一下这两个函数怎么用).
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
const int MAXN = ;
typedef long long LL; LL a[MAXN] , b[MAXN] , c[MAXN] , d[MAXN];
LL num1[MAXN * MAXN] , num2[MAXN * MAXN]; int main()
{
int n;
while(~scanf("%d" , &n)) {
for(int i = ; i < n ; i++) {
scanf("%lld %lld %lld %lld" , a + i , b + i , c + i , d + i);
}
int f = ;
for(int i = ; i < n ; i++) {
for(int j = ; j < n ; j++) {
num1[f] = a[i] + b[j];
num2[f++] = c[i] + d[j];
}
}
LL res = ;
int temp = ;
sort(num2 , num2 + f);
for(int i = ; i < f ; i++) {
temp = lower_bound(num2 , num2 + f , -num1[i]) - num2;
if(temp < f && num2[temp] + num1[i] == ) {
res += upper_bound(num2 , num2 + f , -num1[i]) - num2 - temp;
}
}
cout << res << endl;
}
}
POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)的更多相关文章
- 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(暴力枚举的优化策略)
题目链接: 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 ...
- poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
Description The SUM problem can be formulated . In the following, we assume that all lists have the ...
随机推荐
- HDU 2553 (状压) N皇后问题 (2)
也许大多数做法都是打表,但这里用位运算的思想来解决这个问题,位运算果然强大,Orz 原文地址,感觉讲的很明白了: http://www.cnblogs.com/gj-Acit/archive/2013 ...
- ui/ue设计师应该了解的原型设计软件
前段实践整理过一些原型设计用的软件,这里分享一下,喜欢对更多的PM战线的童鞋有所裨益.(因为交互原型工具Axure ui设计师都很常用了,文中就不专门介绍了) 首先分下类: •1.交互原型(产品能做什 ...
- Object-C 内存管理及对象
关于OC 的内存管理是使用 引用计数的方式 进行管理的引用计数可以使用 办公室的开关灯 来说明 如下图与 OC对象 对应如下
- Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence
Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence 如下代码: for (ind ...
- codevs 1228 苹果树
dfs序+线段树 #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- 20160130.CCPP体系详解(0009天)
程序片段(01):hanoi.c+汉诺塔.c 内容概要:汉诺塔 ///hanoi.c #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...
- opengl截图
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = ; // number of image encoders U ...
- maven整合s2sh截图
- adb shell 查找并删除文件
# -*- coding: cp936 -*- ## function: remove file ## remark: python version -- import os,sys import l ...
- nodejs模拟http-post文件的方法示例
var fs = require("fs"); var http = require('http'); function PostFileToServer(sFileName, d ...