HDU5183Negative and Positive (NP)(哈希表)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5183

题意:

给定一个长度为n的序列,判断是否存在

一段序列 sum(i,j)=
a i ?a i+1 +a i+2 +?+(?1) j?i aj
= k;

分析:

我们维护一段前缀和

sum[j]表示从开始到第j个元素, 即 sum(0,j);

然后我们将其加入到哈希表中

因为不同要么是正负正 。。。。,要么就是负正负。。。。的结构

因此我们维护两个哈希表

表1插入 sum ,表2插入-sum;

当i为奇数的时候在表2中查询 sum-k;

当i为偶数的时候在表1中查询 sum+k

然后判断存不存在就好

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int MAXN=1000010;
const int HASH=1000007;

inline LL read()//输入外挂
{
    char ch=getchar();LL x=0,f=1;
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

struct hashmap//建立哈希表
{
    LL a[MAXN];
    int head[HASH],next[MAXN],size;
    void init(){//初始化
        memset(head,-1,sizeof(head));
        size=0;
    }
    bool find(LL val){//查找一个元素是否在哈希表内
        int tmp = (val%HASH + HASH)%HASH;
        for(int i = head[tmp];i!=-1;i=next[i])
            if(val==a[i]) return true;
        return false;
    }
    void add(LL val){//添加元素到哈希表中
        int tmp =(val%HASH+HASH)%HASH;
        if(find(val)) return;
        a[size]=val;
        next[size]=head[tmp];
        head[tmp]=size++;
    }
}h1,h2;

LL a[MAXN];

int main()
{
    int t,n,cas=1,k;
    t=read();
    while(t--){
        n=read();
        k=read();
        for(int i=0;i<n;i++)
            a[i]=read();
        LL sum=0;
        h1.init(),h2.init();
        h1.add(0),h2.add(0);
        bool flag = 0;
        for(int i=n-1;i>=0;i--){
            if(i&1) sum-=a[i];
            else sum+=a[i];
            if(i%2==0){
                if(h1.find(sum-k)) flag=1;
            }
            else{
                if(h1.find(sum+k)) flag=1;
            }
            h1.add(sum);
            h2.add(-sum);
            if(flag) break;
        }
        printf("Case #%d: ",cas++);
        if(flag)puts("Yes.");
        else puts("No.");
    }
    return 0;
}



时间: 2024-08-25 21:32:33

HDU5183Negative and Positive (NP)(哈希表)的相关文章

hdu 5183. Negative and Positive (哈希表)

Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2177    Accepted Submission(s): 556 Problem Description When given an array (a0,a1,a2,?an−1) and an integer K, you are

hdu5183---Negative and Positive (NP)(HASHMAP)

Problem Description When given an array (a0,a1,a2,?an?1) and an integer K, you are expected to judge whether there is a pair (i,j)(0≤i≤j /************************************************************************* > File Name: hdu5183.cpp > Author: AL

哈希表的简单实现

下面这个散列表的实现来自K&R,很经典.在其他场景中遇到的实现更复杂,基本原理不变,只是在hash算法,或者在快速查询上做了优化. #include <stdio.h> #include <stdlib.h> //具有相同hash值构成的链表 struct nlist{ struct nlist * next; char * name;  //key-定义的名字 char * defn;  //value-替换文本 }; #define HASHSIZE 101  //桶的

C语言实现的哈希表

C语言实现的哈希表哈希表可以简单理解为多个链表的集合,将每个新的成员根据其哈希值进行分类,这样可以加快链表的查找速度参考:https://www.cnblogs.com/s-b-b/p/6208565.html #include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZE 10 typedef unsigned int uint; /*定义一个链表的节点*/ typedef str

数据结构之哈希表

最近看PHP数组底层结构,用到了哈希表,所以还是老老实实回去看结构,在这里去总结一下. 1.哈希表的定义 这里先说一下哈希表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过把关键码映射的位置去寻找存放值的地方,说起来可能感觉有点复杂,我想我举个例子你就会明白了,最典型的的例子就是字典,大家估计小学的时候也用过不少新华字典吧,如果我想要获取“按”字详细信息,我肯定会去根据拼音an去查找 拼音索引(当然也可以是偏旁索引),我们首先去查an在字典的位置,查了一下得到“安”,结果如下.这

哈希函数和哈希表综述 (转)

哈希表及哈希函数研究综述 摘要 随着信息化水平的不断提高,数据已经取代计算成为了信息计算的中心,对存储的需求不断提高信息量呈现爆炸式增长趋势,存储已经成为急需提高的瓶颈.哈希表作为海量信息存储的有效方式,本文详细介绍了哈希表的设计.冲突解决方案以及动态哈希表.另外针对哈希函数在相似性匹配.图片检索.分布式缓存和密码学等领域的应用做了简短得介绍 哈希经过这么多年的发展,出现了大量高性能的哈希函数和哈希表.本文通过介绍各种不同的哈希函数的设计原理以及不同的哈希表实现,旨在帮助读者在实际应用中,根据问

哈希表的原理与实现

[转自]:http://my.oschina.net/chape/blog/132533 目录[-] 哈希表的原理与实现 一致性 hash 算法 基本场景 hash 算法和单调性 consistent hashing 算法的原理 虚拟节点 小结 分布式哈希算法 哈希函数 哈希表 分布式哈希表 哈希表的工作原理与常用操作 基础操作 应用举例 哈希表的原理与实现 一列键值对数据,存储在一个table中,如何通过数据的关键字快速查找相应值呢?不要告诉我一个个拿出来比较key啊,呵呵. 大家都知道,在所

Negative and Positive (NP) ( Hash 维护 )

Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2846 Accepted Submission(s): 782 Problem Description When given an array (a0,a1,a2,?an?1) and an integer K, you are expec

UVA1225DigitCounting(简单哈希表)

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N (1 < N < 10000). After that, he counts the number of times each digit (0 to 9) appears in the sequenc