题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5183

题意:给出一个n个元素的数组,现在要求判断 a1-a2+a3-a4+.....+/-an 中是否存在某个某个区间使得 ai-ai+1+ai+2...+(-1)j-iaj == k??

这个题要利用Hash就可以实现几乎在 O(n) 的时间内实现查找判断.

记录前缀和,然后枚举起点进行判断。分两种情况进行考虑:

1.起点 i 为奇数,那么 a[i]-a[i+1]+a[i+2]....+(-1)^(j-i)*a[j] = sum[j] - sum[i-1] = k ,每次枚举 sum[i-1] + k 如果在hash表中存在 ,就证明存在此区间.

2.起点 i 为偶数,那么 -a[i]+a[i+1]-a[i+2]....+(-1)^(j-i)*a[j] = sum[j] - sum[i-1] = -k ,每次枚举 sum[i-1] - k ,查找hash表。与上一步类似.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = ;
const int H = ;
int Hash[H],cur;
void initHash(){
memset(Hash,-,sizeof(Hash));
cur = ;
}
struct Node{
LL v;
int next;
}node[N];
void insertHash(LL v){
int num = (int)(v%H+H)%H;
node[cur].v = v;
node[cur].next = Hash[num];
Hash[num] = cur++;
}
bool searchHash(LL v){
int num = (int)(v%H+H)%H;
for(int k = Hash[num];k!=-;k=node[k].next){
if(node[k].v==v) return true;
}
return false;
}
LL sum[N];
int main()
{
int tcase,t=;
scanf("%d",&tcase);
while(tcase--){
initHash();
int n,k;
scanf("%d%d",&n,&k);
sum[] = ;
for(int i=;i<=n;i++){
LL x;
scanf("%lld",&x);
if(i%) sum[i] = sum[i-]+x;
else sum[i] = sum[i-]-x;
}
insertHash(sum[n]);
bool flag = false;
for(int i=n;i>=;i--){
if(i%==&&searchHash(sum[i-]+k)){
flag = true;
break;
}
if(i%==&&searchHash(sum[i-]-k)){
flag = true;
break;
}
insertHash(sum[i]);
}
printf("Case #%d: ",t++);
if(flag) printf("Yes.\n");
else printf("No.\n");
}
return ;
}

hdu 5183(Hash处理区间问题)的更多相关文章

  1. hdu 5183 hash表

    BC # 32 1002 题意:给出一个数组 a 和一个数 K ,问是否存在数对( i , j ),使 a i   - a i + 1 +……+ (-1)j - i  a j : 对于这道题,一开始就 ...

  2. hdu 5183

    hdu 5183(Hash处理区间问题) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5183 题意:给出一个n个元素的数组,现在要求判断 a1-a2 ...

  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 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  5. HDU 6046 - hash | 2017 Multi-University Training Contest 2

    思路来自题解和一些博客 最麻烦的是样例没啥用- - /* HDU 6046 - hash [ hash,鸽巢 ] | 2017 Multi-University Training Contest 2 ...

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

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

  7. hdu 5183(hash)

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

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

  9. HDU 2836 (离散化DP+区间优化)

    Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...

随机推荐

  1. vue 学习笔记—路由篇

    一.关于三种路由 动态路由 就是path:good/:ops    这种 用 $route.params接收 <router-link>是用来跳转  <router-view> ...

  2. 20165237 2017-2018-2 《Java程序设计》第8周学习总结

    20165237 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 1.程序:一段静态的代码,它是应用软件执行的蓝本. 2.进程:程序的一次动态执行过程,它对应 ...

  3. go语言time包的学习(Time,Location,Duration,Timer,Ticker)

    package main;   import (     "time"     "fmt" )   func main() {     //time.Time代 ...

  4. python3字典中items()和python2.x中iteritems()有什么不同?

    在Python2.x中: items() 用于返回一个字典的拷贝列表[Returns a copy of the list of all items (key/value pairs) in D],占 ...

  5. 利用 ffmpeg 的 maskedmerge 滤镜, 从视频中提取移动对象

    ffmpeg \ -i background.png \ -i video.mkv \ -filter_complex \ " color=#00ff00:size=1280x720 [ma ...

  6. MySQL索引操作

    创建普通索引 CREATE INDEX index_name ON table_name(column1,column2); 另一种建立方式: ALTER TABLE table_name ADD I ...

  7. mysql:批量插入不同的UUID

    INSERT INTO t_base_role_resource_ref (refID, roleID, resID, orgID, belongTo) SELECT uuid() AS refID, ...

  8. Laravel 怎么在 blade 视图中将带 HTML 字符原样输出

    ### 感觉这是比较细小的,细节处理问题,很容易就一下子想不起怎么处理 但知道处理方式是那么简单时,真的觉得基础不够扎实 ### 富文本编辑内容: 视图原样输出: 视图模板的标签是这样处理就可以的-- ...

  9. python字符串str和字节数组bytes相互转化

    1 引言 后续待补充 2 代码 b = b"Hello, world!" # bytes s = "Hello, world!" # string print( ...

  10. 远程连接阿里云的mysql数据库

    第一步 由于mysql版本问题 先尝试打开 sudo vim /etc/mysql/my.cnf 如空,再尝试打开 sudo vim /etc/mysql/mysql.conf.d/mysqld.cn ...