题目链接:HDU 5183

Problem Description

When given an array \((a_0,a_1,a_2,⋯a_{n−1})\) and an integer \(K\), you are expected to judge whether there is a pair \((i,j)(0≤i≤j<n)\) which makes that \(NP−sum(i,j)\) equals to \(K\) true. Here \(NP−sum(i,j)=a_i−a_{i+1}+a_{i+2}+⋯+(−1)^{j−i}a_j\)

Input

Multi test cases. In the first line of the input file there is an integer \(T\) indicates the number of test cases.

In the next \(2∗T\) lines, it will list the data for each test case.

Each case occupies two lines, the first line contain two integers \(n\) and \(K\) which are mentioned above.

The second line contain \((a_0,a_1,a_2,⋯a_{n−1})\) separated by exact one space.

[Technical Specification]

All input items are integers.

\(0<T≤25,1≤n≤1000000,−1000000000≤a_i≤1000000000,−1000000000≤K≤1000000000\)

Output

For each case,the output should occupies exactly one line. The output format is Case #id: ans, here id is the data number starting from 1; ans is “Yes.” or “No.” (without quote) according to whether you can find \((i,j)\) which makes \(PN−sum(i,j)\) equals to \(K\).

See the sample for more details.

Sample Input

2
1 1
1
2 1
-1 0

Sample Output

Case #1: Yes.
Case #2: No.

Hint

If input is huge, fast IO method is recommended.

Source

BestCoder Round #32

Solution

题意

给定长度为 \(n\) 的数组,问是否存在一段区间其加减交错的和等于 \(k\)。

思路

对该数组求前缀和然后哈希就行。

不过这题不同的 set 过不了。

所以要手写哈希。

当然 unordered_set 也可以过。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7; struct HashMap {
int head[maxn], next[maxn];
ll num[maxn];
int tot;
inline void init() {
tot = 0;
memset(head, -1, sizeof(head));
}
inline void insert(ll val) {
int h = abs(val) % maxn;
num[tot] = val, next[tot] = head[h], head[h] = tot++;
}
inline bool find(ll val) {
int h = abs(val) % maxn;
for(int i = head[h]; ~i; i = next[i]) {
if(num[i] == val) {
return true;
}
}
return false;
}
} hashmap; ll arr[maxn], sum[maxn]; int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
int kase = 0;
while(T--) {
hashmap.init();
ll n, k;
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> arr[i];
sum[i] = sum[i - 1] + (i & 1? arr[i]: -arr[i]);
}
int flag = 0;
for(int i = n; i; --i) {
hashmap.insert(sum[i]);
if(i & 1) {
if(hashmap.find(sum[i - 1] + k)) {
flag = 1;
break;
}
} else {
if(hashmap.find(sum[i - 1] - k)) {
flag = 1;
break;
}
}
}
cout << "Case #" << ++kase << ": " << (flag? "Yes." : "No.") << endl;
}
return 0;
}

HDU 5183 Negative and Positive (NP) (手写哈希)的更多相关文章

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

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

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

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

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

  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. hdu5183Negative and Positive (NP))——手写Hash&&模板

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

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

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

随机推荐

  1. 通过Samba练习metasploit的使用

    文章学习利用metasploit通过Samba3.X获取metasploitable2的shell,从而去熟悉metasploit的使用. 环境: kali IP:192.168.137.133 me ...

  2. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  3. CSS动画划入划出酷炫

    HTML插入 <!DOCTYPE html> <html class="no-js iarouse"> <head> <meta char ...

  4. Pandas处理缺失的数据

    处理丢失数据 有两种丢失数据: None np.nan(NaN) import numpy as np import pandas from pandas import DataFrame 1. No ...

  5. 【记录】ajax 设置请求header的Content-Type 为 application/json;charset=utf8

    具体案例如下 $.ajax({ url: context.state.IpccSendIm, method: 'POST', data: JSON.stringify(val), headers:{' ...

  6. 【转载】关于Maven项目build时出现No compiler is provided in this environment的处理

    参考地址;https://blog.csdn.net/lslk9898/article/details/73836745

  7. rm - 移除文件或者目录

    总览 rm [options] file... POSIX(Portable Operating System Interface 可移植的操作系统接口) 选项: [-fiRr] GNU 选项 (最短 ...

  8. 203-基于ARM和双TI DSP TMS320C6678的6UCPCI高清编解码处理平台

    基于ARM和双TI DSP TMS320C6678的6UCPCI高清编解码处理平台 1.产品简介 该板卡由我公司自主研发,以TI Cortex-A8.TI 双DSP TMS320C6678为设计核心, ...

  9. python浮点数与整数间的转化

    舍弃小数部分 >>> math.trunc(12.533222) 12 >>> round(12.2544) 12 按给定小数位数四舍五入 >>> ...

  10. pandas-同时处理两行数据

    pandas-同时处理两行数据 假设数据集data如下所示: 如果我们想要将user_id 和 item_id两列进行对应元素相加的操作,该怎么办呢? 显然我们先定义一个加法函数,然后使用apply函 ...