根据奇偶开两个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. 认识C++中的临时对象temporary object 分类: C/C++ 2015-05-11 23:20 137人阅读 评论(0) 收藏

    C++中临时对象又称无名对象.临时对象主要出现在如下场景. 1.建立一个没有命名的非堆(non-heap)对象,也就是无名对象时,会产生临时对象. Integer inte= Integer(5); ...

  2. 数迹学——Asp.Net MVC4入门指南(2):添加一个控制器

    自嘲一下......万事还是得从官方的入门开始 由于找不到适合新新手的MVC入门实例所以回过头来做一下微软的 <Asp.Net MVC4入门指南>. 只有把自己放在太阳下暴晒,才知道自己有 ...

  3. 论文阅读之:Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network

    Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network  2016.10.23 摘要: ...

  4. Learning to write a compiler

    http://stackoverflow.com/questions/1669/learning-to-write-a-compiler?rq=1 Big List of Resources: A N ...

  5. spl_autoload_register更改框架文件引用模式

    今天单点登陆要用到 spl_autoload_register,但是YII的Yii::autoload在包含失败的时候会抛异常,就不会执行(spl_autoload_call)其他spl_autolo ...

  6. Nginx 负载均衡学习

    nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器. nginx负载均衡服务器 tomcat1服务器 tomcat2服务器 1.1   ...

  7. HackerRank "New Year Chaos"

    Two tricks here: 1. Counting no. of inversed pairs - using Merge Sort, nothing special 2. How to che ...

  8. 【Oozie】安装配置Oozie

    安装和配置Oozie Oozie用于Hadoop的工作流配置: 参考链接: <Install and Configure Apache Oozie Workflow Scheduler for ...

  9. Django缓存使用方法

    Django缓存分为Session和Cookie:Session为放在服务器端的缓存:Cookie为放在客户端(浏览器)的缓存. Session一般用来保存登录会话:Cookie一般用来保存一些个性化 ...

  10. ruby中rsa加签解签方法

    # coding:utf-8require 'openssl'require 'base64'# rsa签名,文本内容和私钥路径def rsa_sign(data,private_key_path) ...