zoukankan      html  css  js  c++  java
  • hdu-5183-Negative and Positive (NP)(hash模板)

    题目链接

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 typedef long long LL;
     9 
    10 const int MAXN=1000010;
    11 const int HASH=1000007;
    12 
    13 inline LL read()//输入外挂
    14 {
    15     char ch=getchar();LL x=0,f=1;
    16     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    17     while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    18     return x*f;
    19 }
    20 
    21 struct hashmap//建立哈希表
    22 {
    23     LL a[MAXN];
    24     int head[HASH],next[MAXN],size;
    25     void init(){//初始化
    26         memset(head,-1,sizeof(head));
    27         size=0;
    28     }
    29     bool find(LL val){//查找一个元素是否在哈希表内,一个hash值可能对应多个值,在这几个值中查找 
    30         int tmp = (val%HASH + HASH)%HASH;
    31         for(int i = head[tmp];i!=-1;i=next[i])
    32             if(val==a[i]) return true;
    33         return false;
    34     }
    35     void add(LL val){//添加元素到哈希表中
    36         int tmp =(val%HASH+HASH)%HASH;
    37         if(find(val)) return;
    38         a[size]=val;
    39         next[size]=head[tmp];//冲突值对应同一个 hash值 
    40         head[tmp]=size++;//head保存每个值的下标 
    41     }
    42 }h1,h2;
    43 
    44 LL a[MAXN];
    45 
    46 int main()
    47 {
    48     int t,n,cas=1,k;
    49     t=read();
    50     while(t--){
    51         n=read();
    52         k=read();
    53         for(int i=0;i<n;i++)
    54             a[i]=read();
    55         LL sum=0;
    56         h1.init(),h2.init();
    57         h1.add(0),h2.add(0);
    58         bool flag = 0;
    59         for(int i=n-1;i>=0;i--){
    60             if(i&1) sum-=a[i];
    61             else sum+=a[i];
    62             if(i%2==0){
    63                 if(h1.find(sum-k)) flag=1;
    64             }
    65             else{
    66                 if(h1.find(sum+k)) flag=1;
    67             }
    68             h1.add(sum);
    69             h2.add(-sum);
    70             if(flag) break;
    71         }
    72         printf("Case #%d: ",cas++);
    73         if(flag)puts("Yes.");
    74         else puts("No.");
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    更改ubuntu的官方镜像源
    python中的decorator的作用
    thunderbird 设置 邮件回复时内容在上方显示
    Sapnco3 RfcTable Structure
    DbEntry 访问Access2010数据库
    DbEntry 默认 主键ID为long
    DbEntry 简单实现
    nginx学习时使用EditPuls编辑conf.xml
    java的集合类面试题
    tomcat组成介绍和调优方案
  • 原文地址:https://www.cnblogs.com/slothrbk/p/8832472.html
Copyright © 2011-2022 走看看