根据奇偶开两个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. css+div

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. 使用response实现文件的下载

    package cn.itcast.request; import java.io.FileInputStream;import java.io.IOException;import java.io. ...

  3. Oracle 10g提权测试

    一直想摸索一下orcl提权的方式,今天测试了一下10g,可以成功提权. C:\wmpub>sqlplus scott/tiger@orcl SQL*Plus: Release 10.2.0.1. ...

  4. position置顶或某固定位置 兼容ie6ie7

    用absolute来模拟fixed效果: /*相当于正常的position:fixed;top:0 */.sl_fixed_top{bottom:auto;top:0;_bottom:auto;_to ...

  5. Reveal UI 分析工具分析手机 App

    上篇文章介绍了: Reveal UI 分析工具简单使用 这里介绍如何使用 Reveal UI 分析工具来进行手机 App UI 界面的分析. 前提准备: (1)已安装 Reveal 的 Mac (2) ...

  6. jquery 之ajax获取数据

    $.ajax({    url: "http://www.hzhuti.com",    //请求的url地址    dataType: "json",   / ...

  7. *** glibc detected *** malloc(): memory corruption 分类: C/C++ Linux 2015-05-14 09:22 37人阅读 评论(0) 收藏

    *** glibc detected *** malloc(): memory corruption: 0x09eab988 *** 发现是由于memset越界写引起的. 在Linux Server上 ...

  8. Python-select详解(select、epoll)

    select函数操作集合的时候有个要求,要么集合本身是描述符,要么他提供一个fileno()接口,返回一个描述符. I/O多路复用是在单线程模式下实现多线程的效果,实现一个多I/O并发的效果.看一个简 ...

  9. 清华申请退学博士作品:完全用Linux工作

    http://www.cnblogs.com/cbscan/articles/3252872.html 下文地址 http://blog.oldboyedu.com/use-linux/ 按: 尽管我 ...

  10. npm以及gulp相关操作

    在工作流相关的第一篇博客中,我们安装了nodejs的环境,那么nodejs自带的npm是一个功能十分强大的管理器,它已经不仅仅是局限于nodejs的版本管理器了,那么当现在我们可以通过npm来下载我们 ...