根据奇偶开两个hash表来记录后缀和。注意set会被卡,要手写hash表。

  具体见代码:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = + ;
const int HASH = + ;
typedef long long ll;
struct hashmap
{
int head[HASH], nxt[N], size;
ll state[N];
void init()
{
size = ;
memset(head,-,sizeof(head));
}
bool check(ll val)
{
int temp = (val%HASH + HASH) % HASH;
for(int i=head[temp];i!=-;i=nxt[i])
{
if(val == state[i]) return true;
}
return false;
}
void add(ll val)
{
int temp = (val%HASH + HASH) % HASH;
if(check(val)) return;
size ++;
state[size] = val;
nxt[size] = head[temp];
head[temp] = size;
}
}h1,h2; ll a[N]; int main()
{
int T, kase = ;
scanf("%d",&T);
while(T--)
{
h1.init();h2.init();
h1.add();h2.add();
int n,k;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%I64d",a+i);
}
ll sum = ;
bool flag = false;
for(int i=n;i>=;i--)
{
if(i% == ) sum += a[i];
else sum -= a[i];
if(i% == )
{
if(h1.check(sum-k)) flag = true;
}
else if(h2.check(-sum-k)) flag = true;
if(flag) break;
h1.add(sum);
h2.add(-sum);
}
printf("Case #%d: %s.\n",kase++, flag?"Yes":"No");
}
return ;
}

HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)的更多相关文章

  1. 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 ...

  2. hdu 5183 Negative and Positive (NP)

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

  3. 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\ ...

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

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

  5. 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 ...

  6. 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 ...

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

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

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

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

  9. hdu5183Negative and Positive (NP))——手写Hash&&模板

    题意:问是否存在一段区间其加减交错和为K. 显然,我们可以用set保存前缀和,然后枚举一个端点查找.具体的 若在st1中查找 $t$,为 $sum-t=-k$,在st2中则是 $sum-t=k$. 注 ...

随机推荐

  1. 网页闯关游戏(riddle webgame)--SQL注入的潘多拉魔盒

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. web开发初学者往往会忽视一些常见的漏洞 ...

  2. kernel/Makefile

    ## Makefile for the linux kernel.## Note! Dependencies are done automagically by 'make dep', which a ...

  3. hdu4737 A Bit Fun

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4737 暴力可以过啊.O(N^2)的复杂度. #include <cstdio> ]; in ...

  4. iOS红马甲项目Bug总结(2)

    背景:iOS调用相机和访问图库 一.调用相机或图库: -(void)imgviewClick { ALAuthorizationStatus author = [ALAssetsLibrary aut ...

  5. 士兵站队问题sol

    作者:http://www.cnblogs.com/taoziwel/articles/1859577.html 相类似题目:输油管道问题 [问题描述] 在一个划分成网格的操场上,n个士兵散乱地站在网 ...

  6. sql查看数据字典(表结构)

    SELECT (case when a.colorder=1 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, (case when ...

  7. Maven进价:Maven构建错误汇总

    问题:The method of type must override asuperclass? annotation:@Override的原因 办法:项目右键->build path-> ...

  8. java为什么要设置环境变量

    1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相 应的命令程序.我们需要把 jdk安装目录下的bin目录增加到现有的PAT ...

  9. 通过List<String>动态传递参数给 sqlcommand.Parameters

    通过List<String>动态传递参数 private void GetallChecked_TreeNote(TreeNodeCollection aNodes, ref int To ...

  10. gerrit 解决中文乱码相关配置(转载)

    From:http://www.cnblogs.com/Jerryshome/archive/2012/04/19/2457170.html 计划在团队中采用code review,因为一直是用git ...