hdu 5183 Negative and Positive (NP)
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5183
Negative and Positive (NP)
Description
When given an array $\left( {{a_0},{a_1},{a_2}, \cdots {a_{n - 1}}} \right)$ and an integer $K$, you are expected to judge whether there is a pair $(i,j)\ (0 \leq i \leq j < n)$ which makes that $NP−sum(i,j)$ equals to $K$ true. Here $NP-sum(i,j)={a_i}{\rm{ - }}{a_{i{\rm{ + 1}}}}{\rm{ + }}{a_{i{\rm{ + }}2}}{\rm{ + }} \cdots {\rm{ + ( - 1}}{{\rm{)}}^{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 $\left( {{a_0},{a_1},{a_2}, \cdots {a_{n - 1}}} \right)$separated by exact one space.
[Technical Specification]
All input items are integers.
$0 < T \leq 25,1 \leq n \leq 1000000,-1000000000 \leq ai \leq 1000000000,-1000000000 \leq K \leq 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.
哈希大法好。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1000007;
const int INF = 0x3f3f3f3f;
typedef long long ll;
struct Hash_Set {
ll num[N << 1];
int tot, head[N], next[N];
inline void init() {
tot = 0, cls(head, -1);
}
inline void insert(ll val) {
int u = abs(val) % N;
num[tot] = val, next[tot] = head[u], head[u] = tot++;
}
inline bool find(ll val) {
int u = abs(val) % N;
for (int i = head[u]; ~i; i = next[i]) {
if (num[i] == val) return true;
}
return false;
}
}hash;
ll arr[N], sum[N];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
ll k;
int t, n, c = 1;
scanf("%d", &t);
while (t--) {
hash.init();
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + (i & 1 ? arr[i] : -arr[i]);
}
bool f = false;
for (int i = n; i > 0; i--) {
hash.insert(sum[i]);
if (f) break;
if (i & 1) {
if (hash.find(sum[i - 1] + k)) {
f = true;
break;
}
} else {
if (hash.find(sum[i - 1] - k)) {
f = true;
break;
}
}
}
printf("Case #%d: %s\n", c++, f ? "Yes." : "No.");
}
return 0;
}
hdu 5183 Negative and Positive (NP)的更多相关文章
- 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\ ...
- 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)(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) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- 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 ...
- [HDOJ 5183] Negative and Positive (NP) 【Hash】
题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...
- hdu 5183(hash)
传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP− ...
随机推荐
- 普通session vs MemcachedSession vs RedisSession
一.普通session(数据存储在内存中) #!/usr/bin/env python # -*- coding:utf-8 -*- from hashlib import sha1 import o ...
- windows2008 设置会话超时时间
[ 组策略 ]开始-->运行-->gpedit.msc 计算机配置->管理模板->Windows组件->终端服务->会话
- JavaScript DOM编程艺术学习笔记(一)
嗯,经过了一周的时间,今天终于将<JavaScript DOM编程艺术(第2版)>这本书看完了,感觉受益匪浅,我和作者及出版社等等都不认识,无意为他们做广告,不过本书确实值得一看,也值得推 ...
- 洛谷P2728 纺车的轮子 Spinning Wheels
P2728 纺车的轮子 Spinning Wheels 29通过 66提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 一架纺车 ...
- .NET的三种缓存(页面缓存,控件缓存,自定义缓存)
BLL.Area bll = new BLL.Area(); protected void Page_Load(object sender, EventArgs e) { if (Cache[&quo ...
- 【WCF 2】理解WCF框架的简单小实例
导读:上篇博客介绍了WCF框架的整体情况,然后,闲着没事儿,自己做了一个及其简单的WCF框架的例子帮助自己理解.从简单的入手,一步一步深入!本篇博客是介绍怎么用VS2012从头创建一个WCF项目,是一 ...
- html5 图片转base64预览显示
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- LINE最新版6.5.0在iOS上的删除信息取证
iOS: 9.3.2版 LINE: 6.5.0版 取出LINE的数据库 Line.sqlite,路径如下所示: 检视删除的信息,发现还有不少残留,虽然都是片段,但拼拼凑凑总还是能有些蛛丝马迹,毕竟,总 ...
- Android手机指令操作释疑
有人问我一个关于Android手机root与否的问题,她说明明iTools显示已取得root权限,但她就是没法在该手机上运行需要root权限的App如钛备份等等.我告诉她最好的确认方式便是以adb指令 ...
- Unieap3.5-Grid翻页不提示修改
<toolbar export="{defaultType:'server'}" paging="{onPagingModified:ssSettleCheck.o ...