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\), 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
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) (手写哈希)的更多相关文章
- HDU 5183 Negative and Positive (NP) 前缀和+哈希
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- hdu 5183 Negative and Positive (NP)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...
- HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- 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 ...
- 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 ...
- 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 ...
- hdu 5183. Negative and Positive (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- hdu5183Negative and Positive (NP))——手写Hash&&模板
题意:问是否存在一段区间其加减交错和为K. 显然,我们可以用set保存前缀和,然后枚举一个端点查找.具体的 若在st1中查找 $t$,为 $sum-t=-k$,在st2中则是 $sum-t=k$. 注 ...
- [HDOJ 5183] Negative and Positive (NP) 【Hash】
题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...
随机推荐
- java Semaphore信号灯
Semaphore实现的功能就类似2个公用电话,假如有10个人要打电话:那么只能2个人占有电话,8个需要等待.当2个人中 的任何一个人让开后,其中等待的另外8个人中又有一个人可以使用了等待的8个人中可 ...
- 0x3f3f3f3f 0xbfbfbfbf 等的原理及应用
原理 0x的意思其实是十六进制,后面加的数其实就是一个十六进制数. 在十六进制中,我们知道a代表10,b代表11,c代表12,d代表13,e代表14,f代表15. 所以3f3f3f3f这个数用十进制数 ...
- 16、NumPy ——字节交换
NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...
- hdu3664 Permutation Counting(dp)
hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...
- P5468 [NOI2019]回家路线
传送门 看题目一眼斜率优化,然后写半天调不出来 结果错误的 $dfs$ 有 $95$ 分?暴力 $SPFA$ 就 $AC$ 了? 讲讲正解: 显然是斜率优化的式子: 先不考虑 $q_{s_k}$ 的贡 ...
- js IntersectionObserver api
API const options = { root: null, threshold: [0, 0.5, 1], rootMargin: '30px 100px 20px' } var io = n ...
- 修改DbVisualizer的默认快捷键 .
修改SQL提示的步骤如下:1, 编辑dbvis.jar包下的dbvis-actions.xml文件(解压或直接修改)2, 找到以下的代码<actionidref="show-auto- ...
- .ef core 多对对关系的关联方法
最近在用.net core 重构博客,在使用ef core连表查询时,遇到了一些问题.记录一下. 关系:一个博客可以有多个标签,一个标签可以属于多个博客,博客和标签之间存在多对多的关系 下面是实体代码 ...
- shell根据系统当前的时间向用户输出问候信息
- 02.LNMP架构-MySQL源码包编译部署详细步骤
操作系统:CentOS_Server_7.5_x64_1804.iso 部署组件:Cmake+Boost+MySQL 操作步骤: 一.安装依赖组件 [root@localhost ~]# yum -y ...