POJ 2785 (暴力搜索&二分)

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

题目意思就是在一个4*N的矩阵里面 不同的行找出相加为0的数量;

暴力搜索的话四重循环肯定是不行的,但是如果把他分成两部分来搜,复杂度就从n^4到了2*n^2  数据最大4000 所以完全不会超时

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[4005],b[4005],c[4005],d[4005];
int ab[16000005],cd[16000005];
int main()
{
    int n,k=0,l=0,count=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i]>>b[i]>>c[i]>>d[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            ab[k++]=a[i]+b[j];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            cd[l++]=c[i]+d[j];
    sort(ab,ab+k);
    sort(cd,cd+l);
    int x=n*n-1,num=0;
    for(int i=0;i<n*n;i++){
        while(x>=0&&ab[i]+cd[x]>0)
            x--;
        if(x<0)
            break;
        num=x;
        while(ab[i]+cd[num]==0&&num>=0){
            count++;
            num--;
        }
    }
    cout<<count<<endl;
    return 0;
}

二分

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[4001],b[4001],c[4001],d[4001];
int ab[4000*4000+1],cd[4000*4000+1];
int k2;
int check(int x)
{
    int left=1,right=k2-1,mid;
    while (left<=right)
    {
        mid=(left+right)/2;
        if (x==cd[mid])
        {
            int w=0,e=mid;
            while (x==cd[e]&&e<k2)
                e++,w++;
            e=mid-1;
            while (x==cd[e]&&e>0)
                e--,w++;
            return w;
        }
        else if (x<cd[mid])
            right=mid-1;
        else
            left=mid+1;
    }
    return 0;
}
int main()
{
    int t,i,j,q;
    while (~scanf("%d",&t))
    {
        for (i=1;i<=t;i++)
            scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
        memset(ab,0,sizeof(ab));
        memset(cd,0,sizeof(cd));
        int k1=1,sum=0;
        k2=1;
        for (i=1;i<=t;i++)
        {
            for (j=1;j<=t;j++)
            {
                ab[k1++]=a[i]+b[j];
                cd[k2++]=-(c[i]+d[j]);
            }
        }
        sort(cd+1,cd+k2);
        for (i=1;i<k1;i++)
            sum+=check(ab[i]);
        printf("%d\n",sum);
    }
    return 0;
}

  

时间: 2024-11-05 15:58:41

POJ 2785 (暴力搜索&二分)的相关文章

POJ 2329 (暴力+搜索bfs)

Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your pro

POJ 2110 Mountain Walking 暴力搜索+二分

题意:n*n的矩阵 每次能走四个方向,定义路径的花费为:路径中方格的max-min,问从左上到右下的最小花费,n<=100 4个方向不是DAG,不能DP,暴力搜索 每个点最坏情况下走n*n 共n*n个点 O(n^4)TLE二分ans后 枚举下界,则此时知道路径的最小值和最大值从 起点出发把mn<=c<=mx的点都遍历,此时dfs 相当于遍历图,不用回溯,复杂度为O(n^3*logn) #include <iostream> #include <cstring> #

POJ 1166 暴力搜索 即 枚举

e.... 米还是没有读懂题....T_T ..... e.... 这就是传说中的暴力吗....太血腥了....太暴力了...九重for循环....就这么赤裸裸的AC了.... 水是水了点..但是..我也没想到可以这样解..因为每种操作最多只能进行三次 不然就是重复了..所以...附代码: #include<stdio.h>#include<iostream>#include<string.h>#define FOR(x) for (x=0; x<=3; ++x)

POJ 2785 折半搜索

https://vjudge.net/problem/POJ-2785 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 4005 typedef long long l

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks o

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

poj 2785 4 Values whose Sum is 0(sort+二分)

题意: 给你ABCD四个集合,集合中数的个数都为N(N<=4000),如果分别在ABCD四个集合中取一个数,a b c d ,求有多少种可能使得a+b+c+d=0. 当然你可以尝试枚举所有的组合,绝对可以计算出结果,大概有N^4种吧,如果你有足够的时间还是可以算出来的,哈哈. 当然我不是用上面一种方法计算的,那样算肯定超时. 我的做法是求出所有a+b 到ab数组中, 和所有 c+d到cd数组中,然后排序,枚举每个ab,用二分在cd中查找有没有可能组成0.  有个问题就是二分只能返回一个结果,所以

G - Shuffle&#39;m Up POJ 3087 模拟洗牌的过程,算作暴力搜索也不为过

G - Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3087 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by start