hdu 5183(Hash处理区间问题)

题目链接: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 = 1000005;
const int H = 1000007;
int Hash[H],cur;
void initHash(){
memset(Hash,-1,sizeof(Hash));
cur = 0;
}
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!=-1;k=node[k].next){
if(node[k].v==v) return true;
}
return false;
}
LL sum[N];
int main()
{
int tcase,t=1;
scanf("%d",&tcase);
while(tcase--){
initHash();
int n,k;
scanf("%d%d",&n,&k);
sum[0] = 0;
for(int i=1;i<=n;i++){
LL x;
scanf("%lld",&x);
if(i%2) sum[i] = sum[i-1]+x;
else sum[i] = sum[i-1]-x;
}
insertHash(sum[n]);
bool flag = false;
for(int i=n;i>=1;i--){
if(i%2==1&&searchHash(sum[i-1]+k)){
flag = true;
break;
}
if(i%2==0&&searchHash(sum[i-1]-k)){
flag = true;
break;
}
insertHash(sum[i]);
}
printf("Case #%d: ",t++);
if(flag) printf("Yes.\n");
else printf("No.\n");
}
return 0;
}

hdu 5183的更多相关文章

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

  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(Hash处理区间问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5183 题意:给出一个n个元素的数组,现在要求判断 a1-a2+a3-a4+.....+/-an 中是否 ...

  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) ——(后缀和+手写hash表)

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

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

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

  8. hdu 5183(hash)

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

  9. hdu 5183 hash表

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

随机推荐

  1. 动态生成table 列

    table.render({ elem: '#test-table-comelist' ,url: layui.setter.base + 'list/comelist' ,cols: [[]] ,d ...

  2. sql 找出不包含字母、不包含汉字的数据

    --1.不包含字母 SELECT * FROM t WHERE str NOT LIKE '%[a-zA-Z]%' SELECT * FROM t --2.不包含汉字 SELECT * FROM t ...

  3. 【XSY2925】cti 网络流

    题目描述 有一个 \(n\times m\)的网格,每个格子里面可能有一些炮塔,或者有几个人. 每个炮塔可以在给定的方向(上下左右)上选一个点作为它的攻击位置,然后消灭这个格子里面的所有人.当然也可以 ...

  4. intellij 操作

    默认快捷键 ctrl+alt+l 格式化代码 alt+insert代码自动生成 代码生成 编辑框右键>generator>选择

  5. 工作环境换成Ubuntu18.04小记

    Linux汇总:https://www.cnblogs.com/dunitian/p/4822808.html#linux Ubuntu常用软件安装(小集合)http://www.cnblogs.co ...

  6. function Language

    什么是函数式语言: 函数式语言(functional language)一类程序设计语言.是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据 ...

  7. Spark Standalone spark-env.sh

    export JAVA_HOME=/app/jdk export SPARK_MASTER_PORT=7077 export SPARK_MASTER_WEBUI_PORT=8080 export S ...

  8. javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/websocket/{sid}] : existing endpoint was class com.sanyi.qibaobusiness.framework.webSocket.WebSocketServe

    报错: javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/w ...

  9. Docker下安装GitLab

    1.需要先安装Docker和Docker Compose,参考:https://www.cnblogs.com/hackyo/p/9280042.html 2.配置GitLab SSL(可跳过): m ...

  10. Linux 动态加载共享库