POJ 2785 4 Values whose Sum is 0(折半枚举)
给出四个长度为n的数列a,b,c,d,求从这四个数列中每个选取一个元素后的和为0的方法数。n<=4000,abs(val)<=2^28.
考虑直接暴力,复杂度O(n^4).显然超时。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int a[N], b[N], c[N], d[N];
VI vis; int main ()
{
int n;
LL ans=;
n=Scan();
FOR(i,,n) a[i]=Scan(), b[i]=Scan(), c[i]=Scan(), d[i]=Scan();
FOR(i,,n) FOR(j,,n) vis.pb(-c[i]-d[j]);
sort(vis.begin(),vis.end());
FOR(i,,n) FOR(j,,n) {
int temp=a[i]+b[j];
ans+=upper_bound(vis.begin(),vis.end(),temp)-lower_bound(vis.begin(),vis.end(),temp);
}
printf("%lld\n",ans);
return ;
}
枚举a,二分b+c+d.复杂度O(n+n^3*log(n^3)+n*log(n^3))~O(n^3*logn).
枚举a+b,二分b+c.复杂度O(n^2+n^2*log(n^2)+n^2*log(n^2))~O(n^2*logn).
枚举a+b+c,二分d.复杂度O(n^3+logn+n^3*logn)~O(n^3*logn).
另外此题map常数大过不了。
POJ 2785 4 Values whose Sum is 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(折半枚举 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 ...
- 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 ...
随机推荐
- c++动态库封装及调用(2、windows下动态库创建)
DLL即动态链接库(Dynamic-Link Libaray)的缩写,相当于Linux下的共享对象.Windows系统中大量采用了DLL机制,甚至内核的结构很大程度依赖与DLL机制.Windows下的 ...
- Codeforces Round #460 (Div. 2) 前三题
Problem A:题目传送门 题目大意:给你N家店,每家店有不同的价格卖苹果,ai元bi斤,那么这家的苹果就是ai/bi元一斤,你要买M斤,问最少花多少元. 题解:贪心,找最小的ai/bi. #in ...
- BZOJ1879_Bill的挑战_KEY
题目传送门 第一次看题目感觉毫无还手之力,一看M的范围≤15,果断状压. 但是状压的想法比较新奇. 先想到的状压是设f[i][j]表示前i个状态为j时的方案总数,但是后来想了一想不行,会超时. 于是以 ...
- 南京Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- OpenCV代码提取:dft函数的实现
The Fourier Transform will decompose an image into its sinus and cosines components. In other words, ...
- LeetCode:34. Search for a Range(Medium)
1. 原题链接 https://leetcode.com/problems/search-for-a-range/description/ 2. 题目要求 给定一个按升序排列的整型数组nums[ ]和 ...
- Omad群组部署、依赖部署一键解决
本文来自网易云社区 作者:李培斌 前言 基于omad部署平台实现一键部署的实践已有很多成功的经验,杭研QA的技术先锋们也在ks圈里有很多不同的文章去阐述关于这类需求的实现和思路,当然包括我们金融事业部 ...
- Linux命令应用大词典-第35章 终端
35.1 tty:显示当前连接到当前标准输入的终端设备文件名 35.2 consoletype:显示连接到标准输入的控制台类型 35.3 fgconsole:显示活动的虚拟终端数量 35.4 ming ...
- 【WXS全局对象】Global
Global对象的方法调用时,无需使用 Global.parseInt(...),而是直接使用 parseInt(...) 方法: 名称 说明 parseInt(string, radix) 解析一个 ...
- 【shell 练习2】产生随机数的方法总结
一.产生随机数 ()RANDOM 产生随机数 [root@localhost ~]# echo $RANDOM [root@localhost ~]# )) #想要生成八个随机数,随便加一个八位的数字 ...