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];
    }

}hash;

这是一个hash 结构体  在结构体内 对 [] 进行了重载, 则可以直接用  hash[10000000000] ++; 就标记成功了。

在查询的时候 直接可以  int  x = hash[10000000000] ;  即可 很方便吧 哈哈 也就是说 这个 hash 直接实现了对 特别大的数的标记。


好像没A,概率论啊,烦死了。

#include<stdio.h>
#include<iostream>
#include<map>
#include<vector>
using namespace std;
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];
    }
}hash;
int main()
{
    int n,kase = 0;
    cin>>n;
    while(n--)
    {
        if(kase) printf("\n");
        vector<int> v1,v2,v3,v4,v5,v6;
        int m,ji = 0;
        cin>>m;
        while(m--)
        {
            int temp;
            cin>>temp;
            v1.push_back(temp);
            cin>>temp;
            v2.push_back(temp);
            cin>>temp;
            v3.push_back(temp);
            cin>>temp;
            v4.push_back(temp);
        }
        int len1 = v1.size(),len2 = v2.size(),len3 = v3.size(),len4 = v4.size();
        hash.clear();
        for(int i = 0;i < len1;i++)
            for(int j = 0;j < len2;j++)
                hash[v1[i] + v2[j]]++;
        for(int i = 0;i < len3;i++)
            for(int j = 0;j < len4;j++)
                ji += hash[-v3[i] -v4[j]];       printf("%d\n",ji);
        kase = 1;
    }
    return 0;
}
时间: 2024-10-13 22:50:32

UVa1152 - 4 Values whose Sum is 0(hash)的相关文章

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 <algo

[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