uva1152 - 4 Values whose Sum is 0(hash或STL技巧ac)

题目大意:给定4个n(1 <= n <= 4000)元素集合A, B, C, D,要求分别从中选取一个元素a, b, c, d,使得a+b+c+d = 0,问有多少种选法。

method 1:

这里用到一个很实用的技巧:

  求长度为n的有序数组a中的数k的个数num?

    num=upper_bound(a,a+n,k)-lower_bound(a,a+n,k);

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

const int maxn=4000+10;

int T,n,A[maxn],B[maxn],C[maxn],D[maxn],sum[maxn*maxn];long long ans=0;

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>T;
    while(T--){
        ans=0;
        cin>>n;
        memset(sum,0,sizeof(sum));
        for(int i=0;i<n;i++){
            cin>>A[i]>>B[i]>>C[i]>>D[i];
        }
        int cnt=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                sum[cnt++]=A[i]+B[j];
            }
        }
        sort(sum,sum+cnt);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                ans+=upper_bound(sum,sum+cnt,-C[i]-D[j])-lower_bound(sum,sum+cnt,-C[i]-D[j]);
            }
        }
        cout<<ans<<endl;
        if(T) cout<<endl;
    }

    return 0;
}

method 2:运用hash的思想来做,类似于hdu1496 http://www.cnblogs.com/Fy1999/p/9355319.html

但需要加一个hash的结构体,否则会导致超时

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

const int maxn=4000+10;

int T,n,A[maxn],B[maxn],C[maxn],D[maxn];
long long ans=0;

struct HashMAP{
    static const int mask = 0x7fffff;
    int p[8388608],q[8388608];
    void Clear()
    {
        for(int i=0;i<=mask; ++ i) q[i]=0;
    }
    int& operator [] (int k){
        int i;
        for(i=k&mask; q[i]&&p[i]!=k;i=(i+1)&mask) ;
            p[i]=k;
        return q[i];
    }
}Hash;

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>T;
    while(T--){
        Hash.Clear();//忘记清零导致TLE了。。。TLE不是WA有点坑啊。。。。。。
        ans=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++){
                Hash[A[i]+B[j]]++;
            }
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                ans+=Hash[-(C[i]+D[j])];
            }
        }
        cout<<ans<<endl;
        if(T) cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Fy1999/p/9385926.html

时间: 2024-11-08 23:00:49

uva1152 - 4 Values whose Sum is 0(hash或STL技巧ac)的相关文章

UVa1152 - 4 Values whose Sum is 0(hash)

hash结构体 struct Hash_map { static const int mask=0x7fffff; int p[8388608],q[8388608]; void clear(){ for(int i=0;i<=mask;++i) q[i]=0; } int & operator [] (int k){ int i; for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask); p[i]=k; return q[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

uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

UVa1152 4 Values whose Sum is 0 (中途相遇法)

链接:http://vjudge.net/problem/36014 分析:先枚举a和b,把所有a+b记录下来放在一个有序数组中,然后枚举c和d,查一查-c-d有多少种方法写成a+b的形式(二分查找). 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 4000 + 5; 6 7 int a[maxn], b[maxn], c[maxn], d[maxn];

1152 - 4 Values whose Sum is 0(好用的hash标记,多重循环简单化)

不得不说这个题就是炫酷啊! 首先说一说思路吧,是这么想的: 1.弄四重循环,爆破,明显会超时. 2.为了处理多重循环,就枚举a+b+c,只需要在d中找到a+b+c的相反数即可,超时 3.枚举a+b,只需要在c+d中找到a+b的相反数即可,TMD超时! 4.循环只能优化到这个程度了,再优化就得用哈希表直接调用了. 这个题的哈希表也是新的知识,由于本题a+b的值可能很大很大,所以以前的哈希表检索不好用了(因为数组开不了那么大),多亏高神的Blog,学了一招,哈哈,下面是搬运的内容: 对于一个无法用数

POJ 2785 4 Values whose Sum is 0

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 22691   Accepted: 6869 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

POJ 2785 4 Values whose Sum is 0 [二分]

传送门 13773503 njczy2010 2785 Accepted 25248K 7079MS G++ 1423B 2015-01-11 10:26:48 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 16102   Accepted: 4659 Case Time Limit: 5000MS Description The SUM problem can be

POJ 2785 4 Values whose Sum is 0(折半枚举)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 17088   Accepted: 4998 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

4 Values whose Sum is 0(二分)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 21370   Accepted: 6428 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