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 ...
随机推荐
- 2、单线性变量的回归(Linear Regression with One Variable)
2.1 模型表示 我们通过一个例子来开始:这个例子是预测住房价格的,我们要使用一个数据集,数据集包含俄勒冈州波特兰市的住房价格.在这里,我要根据不同房屋尺寸所售出的价格,画出我的数据集.比方说,如果你 ...
- Codeforces - 1194E - Count The Rectangles - 扫描线
https://codeforc.es/contest/1194/problem/E 给5000条正常的(同方向不会重叠,也不会退化成点的)线段,他们都是平行坐标轴方向的,求能组成多少个矩形. 先进行 ...
- hdu5857 Median(模拟)
Median Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- P4390 [BOI2007]Mokia 摩基亚
传送门 对于一个询问 $(xa,ya),(xb,yb)$,拆成 $4$ 个询问并容斥一下 具体就是把询问变成求小于等于 $xb,yb$ 的点数,减去小于等于 $xa-1,yb$ 和小于等于 $xb,y ...
- 缓存模块redis
1.安装 安装 下载 :wget http://download.redis.io/releases/redis-3.2.8.tar.gz 解压:tar xzf redis-3.2.8.tar.gz ...
- C#面试 笔试题 二
1.using关键字有什么用?什么是IDisposable? using可以声明namespace的引入,还可以实现非托管资源的释放,实现了IDisposiable的类在using中创建,using结 ...
- 录制rtsp音视频
1.使用ffmpeg来录制rtsp视频 视频 ffmpeg -y -i rtsp://172.16.23.66:554/h264major -vcodec copy -f mp4 record.mp4 ...
- Win10电脑查看已连接过WiFi密码的命令
运行中输入CMD,回车,打开命令行窗口. 输入:netsh wlan show profiles 执行后,会列出搜友已连接过的WiFi名字: 输入:netsh wlan show profile ...
- 02 | 日志系统:一条SQL更新语句是如何执行的? 学习记录
<MySQL实战45讲>02 | 日志系统:一条SQL更新语句是如何执行的? 学习记录http://naotu.baidu.com/file/ad320c7a0e031c2d6db7b5a ...
- 解决 docker run 报错 oci runtime error
在部署新服务器运行docker镜像的时候遇到了报错,记录下解决方法. docker 启动容器报错:Error response from daemon: oci runtime error: cont ...