题目链接

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long LL; const int MAXN=;
const int HASH=; inline LL read()//输入外挂
{
char ch=getchar();LL x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
} struct hashmap//建立哈希表
{
LL a[MAXN];
int head[HASH],next[MAXN],size;
void init(){//初始化
memset(head,-,sizeof(head));
size=;
}
bool find(LL val){//查找一个元素是否在哈希表内,一个hash值可能对应多个值,在这几个值中查找
int tmp = (val%HASH + HASH)%HASH;
for(int i = head[tmp];i!=-;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];//冲突值对应同一个 hash值
head[tmp]=size++;//head保存每个值的下标
}
}h1,h2; LL a[MAXN]; int main()
{
int t,n,cas=,k;
t=read();
while(t--){
n=read();
k=read();
for(int i=;i<n;i++)
a[i]=read();
LL sum=;
h1.init(),h2.init();
h1.add(),h2.add();
bool flag = ;
for(int i=n-;i>=;i--){
if(i&) sum-=a[i];
else sum+=a[i];
if(i%==){
if(h1.find(sum-k)) flag=;
}
else{
if(h1.find(sum+k)) flag=;
}
h1.add(sum);
h2.add(-sum);
if(flag) break;
}
printf("Case #%d: ",cas++);
if(flag)puts("Yes.");
else puts("No.");
}
return ;
}

hdu-5183-Negative and Positive (NP)(hash模板)的更多相关文章

  1. hdu 5183 Negative and Positive (NP)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...

  2. HDU 5183 Negative and Positive (NP) (手写哈希)

    题目链接:HDU 5183 Problem Description When given an array \((a_0,a_1,a_2,⋯a_{n−1})\) and an integer \(K\ ...

  3. hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)

    题意: When given an array (a0,a1,a2,⋯an−1) and an integer K, you are expected to judge whether there i ...

  4. HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)

    根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...

  5. HDU 5183 Negative and Positive (NP) 前缀和+哈希

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  6. HDU 5183 Negative and Positive (NP) --Hashmap

    题意:问有没有数对(i,j)(0<=i<=j<n),使得a[i]-a[i+1]+...+(-1)^(j-i)a[j]为K. 解法:两种方法,枚举起点或者枚举终点. 先保存前缀和:a1 ...

  7. HDU 5183 Negative and Positive (NP) (hashmap+YY)

    学到了以邻接表方式建立的hashmap 题意:给你一串数a和一个数k,都有正有负,问知否能找到一对数(i,j)(i<=j)保证a [i] - a [i+1] + a [i+2] - a [i+3 ...

  8. hdu 5183. Negative and Positive (哈希表)

    Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  9. [HDOJ 5183] Negative and Positive (NP) 【Hash】

    题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...

  10. hdu 5183(hash)

    传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP− ...

随机推荐

  1. 【转】Python爬虫_示例2

    爬虫项目:爬取并筛选拉钩网职位信息自动提交简历   一 目标站点分析 #一:实验前准备: 浏览器用Chrome 用Ctrl+Shift+Delete清除浏览器缓存的Cookie 打开network准备 ...

  2. 系统非正常关机启动后出现:an error occurred during the file system

    现象描述: 1.系统ssh登录报Too many open files in system,系统登录不进去,就直接强制关机了,开机后出现(2)的错误: 由于文件描述符用完了,需要把fs.file-ma ...

  3. des加密——补齐

    下面这个网址(英文)介绍的比较全面. http://www.di-mgt.com.au/cryptopad.html

  4. 02_虚拟机的安装和SecureCRT、FileZilla、Xmanage、UltraEdit工具的介绍

    上述几个工具连接不成功的情况,很多时候是因为ssh服务没有安装,CentOS默认安装,不会出现问题,Ubuntu桌面版默认没有安装,需要手动安装,安装部分参考下文SecureCRT部分 一.安装Cen ...

  5. 结合canvas做雨滴特效

    雨滴特效 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  6. R语言的输出函数cat,sink,writeLines,write.table

    根据输出的方向分为输出到屏幕和输出到文件. 1.cat函数即能输出到屏幕,也能输出到文件. 使用方式:cat(... , file = "", sep = " " ...

  7. linux之下载工具wget

    常用格式:wget options URL -b,  --background        启动后转入后台执行 -c,  --continue               接着下载没下载完的文件 - ...

  8. myisam表修复

     数据库myisam引擎表损坏修复步骤:   1.进入到表目录文件下 # myisamchk -of comments.MYI 2. # myisamchk -r  comments.MYI 3. # ...

  9. Qt下TCP编程

    一.服务器 1.声明一个QTcpServer对象 QTcpServer* serverListener; 2.new出对象 this->serverListener = new QTcpServ ...

  10. 高通Android display分析【转】

    本文转载自:http://blog.csdn.net/zhangchiytu/article/details/6777039 高通7系列硬件架构分析 如上图,高通7系列 Display的硬件部分主要由 ...