poj 2785(折半枚举+二分搜索)
题意:
给定 n 行数,每行都有 4 个数A,B,C,D。
要从每列中各抽取出一个数,问使四个数的和为0的所有方案数。
相同数字不同位置当作不同数字对待。
题解:
如果采用暴力的话,从4个数列中选择数组合,共有(N^4)种选择,故时间复杂度为O(N^4),指定会超时。
但,如果将它们分成 AB,CD两组,每组只有 N^2 个组合,而 N 的数据范围为 N < 4000,故采用此种方法不会超时。
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=+; int n;
int a[maxn*maxn];
int b[maxn*maxn];
int num[maxn][]; ll Solve()
{
int index=;
for(int i=;i <= n;++i)
for(int j=;j <= n;++j)
{
a[index]=num[i][]+num[j][];//存储所有的A+B的值
b[index]=num[i][]+num[j][];//存储所有的C+D的值
index++;
}
sort(a+,a+index);
sort(b+,b+index);
ll res=;
/**
A+B+C+D=0 -> C+D=-(A+B)
而C+D的所有值已经预处理好,故可通过二分查找存在于b[]中 -(A+B) 的个数即可
**/
for(int i=;i < index;++i)
{
int t=lower_bound(b+,b+index,-a[i])-b;
int k=;
while(t < index && b[t] == -a[i])//查找 -(A+B) 的个数
k++,t++;
res += k;
}
return res;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i <= n;++i)
for(int j=;j <= ;++j)
scanf("%d",&num[i][j]);
printf("%lld\n",Solve());
}
return ;
}
poj 2785(折半枚举+二分搜索)的更多相关文章
- 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 intege ...
- POJ 3977 折半枚举
链接: http://poj.org/problem?id=3977 题意: 给你n个数,n最大35,让你从中选几个数,不能选0个,使它们和的绝对值最小,如果有一样的,取个数最小的 思路: 子集个数共 ...
- Subset POJ - 3977(折半枚举+二分查找)
题目描述 Given a list of N integers with absolute values no larger than 10 15, find a non empty subset o ...
- poj 3977 Subset(折半枚举+二进制枚举+二分)
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5721 Accepted: 1083 Descripti ...
- POJ 3977 Subset(折半枚举+二分)
SubsetTime Limit: 30000MS Memory Limit: 65536KTotal Submissions: 6754 Accepted: 1277 D ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- NYOJ 1091 超大01背包(折半枚举)
这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...
- poj1840 Eqs(hash+折半枚举)
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- 折半枚举(双向搜索)poj27854 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 23757 Accep ...
随机推荐
- ZooKeeper 典型的应用场景——及编程实现
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- Python-习题-11
1,内容回顾列表:增 append insert extend 删 remove pop clear del 改 li[索引] = '被修改的内容' li[切片]:'被修改的内容' 查 for循环 r ...
- 雅思听听app
最近本人呢,正在紧张的备战雅思考试,因为英语基础很弱,尤其是听力,所以老师推荐了雅思听听这个app,说是特别好使,用了一个多月的,总体来说感觉还是很nice的,但是还有一些小毛病,不过这小毛病瑕不掩瑜 ...
- win10下安装GLPK
认识GLPK GLPK是一个解决线性规划问题的工具.是GNU计划下一个用于解线性规 划(Linear Programming)的工具包.它可以方便的描述线性规划问题,并给出相应解. 因此在linux系 ...
- spring mvc的工作原理
该文转载自:http://blog.csdn.net/u012191627/article/details/41943393 SpringMVC框架介绍 1) spring MVC属于SpringFr ...
- PAT 1037 在霍格沃茨找零钱
https://pintia.cn/problem-sets/994805260223102976/problems/994805284923359232 如果你是哈利·波特迷,你会知道魔法世界有它自 ...
- Character Encoding Issues for tomcat
https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8 https://stackoverflow.com/questions/10936846 ...
- HTML5的placeHolder在IE9下workaround引发的Bug(按下葫芦起了瓢)
详见StackOverFlow的:Simple jQuery form Validation: Checking for empty .val() failing in ie9 due to plac ...
- Axios插件和loading的实现
axios插件就是一个ajax插件 axios具有ajax的所有方法如 get post delete put等等的方法 使用时只需要引入即可 如import Axios form 'axios' 不 ...
- FuelPHP 系列(六) ------ CURD 增删改查
一.create $article = new Model_Article(); // 或 $article = Model_Article::forge(); // 保存数据,返回新增数据 id $ ...