poj2785双向搜索

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 2 28 ) 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
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

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).

题意:求四个数的和为0的情况有几种

题解:折半枚举+sort,,很重要的小技巧:upper_bound(a,a+n,s)-low_bound(a,a+n,s)表示a数组(已排序)里等于s的个数,

刚开始用if(all[lower_bound(all,all+n*n,-a[i]-b[j])-all]==-a[i]-b[j])处理wa了,发现原来是因为只算了一个相等的情况,要是有几个同时等于就漏了

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const double g=10.0,eps=1e-9;
const int N=4000+5,maxn=10000+5,inf=0x3f3f3f3f;

ll a[N],b[N],c[N],d[N];
ll all[N*N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    ll n;
    while(cin>>n){
        for(ll i=0;i<n;i++)cin>>a[i]>>b[i]>>c[i]>>d[i];
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<n;j++)
            {
                all[i*n+j]=c[i]+d[j];
            }
        }
        sort(all,all+n*n);
        ll ans=0;
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<n;j++)
            {
                ans+=upper_bound(all,all+n*n,-a[i]-b[j])-lower_bound(all,all+n*n,-a[i]-b[j]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

时间: 2024-08-03 10:58:15

poj2785双向搜索的相关文章

FZU 11月月赛D题:双向搜索+二分

/* 双向搜索感觉是个不错的技巧啊 */ 题目大意: 有n的物品(n<=30),平均(两个人得到的物品差不能大于1)分给两个人,每个物品在每个人心目中的价值分别为(vi,wi) 问两人心目中的价值差最小是多少. 分析: 直接暴搜目测会超时 想到先搜索前一半,用数组a[0][i]保存第一个人在前半段取 i 个物品两个人的差的所有情况: 再搜索后一半保存两个人的差的相反数,用相同的规则保存在a[1][]中. 要想总差最小只需要 a[0][i]-a[1][num-i] (num=n/2或 n/2+1)

NOIP2002 字串变换题解(双向搜索)

65. [NOIP2002] 字串变换 时间限制:1 s   内存限制:128 MB [问题描述] 已知有两个字串A$, B$及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在A$中的子串A1$可以变换为B1$.A2$可以变换为B2$-. 例如:A$='abcd'  B$='xyz' 变换规则为:'abc'->'xu' 'ud'->'y' 'y'->'yz' 则此时,A$可以经过一系列的变换变为B$,其变换的过程为: 'abc

折半枚举 双向搜索

从四个数列选择的话共有n的4次方种情况.将它们对半分成ab和cd再考虑以快速解决. 从两个数列中选择只有n的2次方种情况,可以进行枚举. 从c和d中取数字的n的平方种方法全都枚举出来排好序.运用二分搜索从中搜索-a[i]-b[j].复杂度O(n^2logn). #include<iostream> #include<math.h> #include<algorithm> #include<stdio.h> using namespace std; int n

POJ2785: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 s

POJ2785 4 Values whose Sum is 0 【枚举】

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 16000   Accepted: 4625 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how

北大ACM2785——4 Values whose Sum is 0~~双向搜索

题目的意思很明确,ABCD四个集合,从各个集合中抽取一个数,求将四个数相加等于0的组合数. 由于N最大达到4000,所以,暴力的方法过不了,就是时间限制为15000MS也是不行. 不过我们可以双向搜索,也就是折半搜索. 先求出CD这两个集合的两两配对的新的集合CD,然后在从CD集合中找AB集合两两配对的和k的负数有多少个.用upper_bound和lower_bound函数可以很快的求解出来. 简单来说,a+b+c+d=0,先算出c + d,a + b = -(c + d),在集合CD中查找.

搜索之双向搜索

双向搜索是为了避免在深层子树上浪费时间 有的问题有初态 和 终态 当我们从初态和终态双向搜索时,就相当已经搜索了整个状态空间 来看一个例题吧 达达帮翰翰给女生送礼物,翰翰一共准备了N个礼物,其中第i个礼物的重量是G[i]. 达达的力气很大,他一次可以搬动重量之和不超过W的任意多个物品. 达达希望一次搬掉尽量重的一些物品,请你告诉达达在他的力气范围内一次性能搬动的最大重量是多少. 输入格式 第一行两个整数,分别代表W和N. 以后N行,每行一个正整数表示G[i]. 输出格式 仅一个整数,表示达达在他

[poj2785]4 Values whose Sum is 0(hash或二分)

4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: 5778 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how man

POJ2785 4 Values whose Sum is 0(二分)

题目链接: http://poj.org/problem?id=2785 题意: 给定你四个大小为n的集合 然后又多少个四元组的和为0,分别来自这四个集合: 分析: 分成两组,得到sum1[[,sum2[],然后排下序,在sum2中二分搜索-sum1[i]记下数就可以了 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using names