折半枚举(双向搜索)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 ...
随机推荐
- Ice Cave-CodeForces(广搜)
链接:http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on ...
- MongoDB小结23 - 索引简介
MongoDB中的索引,可以看作是书的目录. 想象一下给你一本没有目录的书,然后让你去查询指定内容,我只想说,我不是电脑,我很蛋疼! 让你翻没有目录的书,就跟让电脑查询没有索引的集合一样,从头查询到尾 ...
- maven打包插件maven-shade-plugin简单介绍
作用: 1.可以把依赖打入jar包,然后直接使用这个jar包,从而不用担心依赖问题 2.通过设置MainClass,创建一个可以执行的jar包 3.Java工程经常会遇到第三方 Jar 包冲突,使用 ...
- pipenv 的使用
pipenv 的使用 学习了:https://blog.csdn.net/chroming/article/details/77104873?locationNum=4&fps=1 https ...
- CentOS 5.11开启VNC Service
1. #yum install vncserver 2. #vncpasswd 此密码将成为vnc的login password password: ...
- [Spring Boot ] Creating the Spring Boot Project : Demo: Creating a REST Controller
In Spring boot, define a REST API endpoint is pretty easy. package com.globomatisc.bike.controllers; ...
- [tarjan] 1827 Summer Holiday
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1827 Summer Holiday Time Limit: 10000/1000 MS (Java/ ...
- 算法基础:正整数指定规则排序问题(Golang实现)
给定字符串内有非常多正整数,要求对这些正整数进行排序.然后返回排序后指定位置的正整数 排序要求:依照每一个正整数的后三位数字组成的整数进行从小到大排序 1)假设不足三位,则依照实际位数组成的整数进行比 ...
- HttpUtils 发送http请求工具类
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URISyntaxEx ...
- IE6\7\8 :last-child 和 :first-chlid 兼容
IE9以下不支持last-child ,只支持first-child,边框尽量用上边框.