题目连接

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.

哈希大法好。。

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<vector>
  7. #include<queue>
  8. #include<set>
  9. using std::set;
  10. using std::sort;
  11. using std::pair;
  12. using std::swap;
  13. using std::queue;
  14. using std::multiset;
  15. #define pb(e) push_back(e)
  16. #define sz(c) (int)(c).size()
  17. #define mp(a, b) make_pair(a, b)
  18. #define all(c) (c).begin(), (c).end()
  19. #define iter(c) decltype((c).begin())
  20. #define cls(arr, val) memset(arr, val, sizeof(arr))
  21. #define cpresent(c, e) (find(all(c), (e)) != (c).end())
  22. #define rep(i, n) for(int i = 0; i < (int)n; i++)
  23. #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
  24. const int N = 1000007;
  25. const int INF = 0x3f3f3f3f;
  26. typedef long long ll;
  27. struct Hash_Set {
  28. ll num[N << 1];
  29. int tot, head[N], next[N];
  30. inline void init() {
  31. tot = 0, cls(head, -1);
  32. }
  33. inline void insert(ll val) {
  34. int u = abs(val) % N;
  35. num[tot] = val, next[tot] = head[u], head[u] = tot++;
  36. }
  37. inline bool find(ll val) {
  38. int u = abs(val) % N;
  39. for (int i = head[u]; ~i; i = next[i]) {
  40. if (num[i] == val) return true;
  41. }
  42. return false;
  43. }
  44. }hash;
  45. ll arr[N], sum[N];
  46. int main() {
  47. #ifdef LOCAL
  48. freopen("in.txt", "r", stdin);
  49. freopen("out.txt", "w+", stdout);
  50. #endif
  51. ll k;
  52. int t, n, c = 1;
  53. scanf("%d", &t);
  54. while (t--) {
  55. hash.init();
  56. scanf("%d %lld", &n, &k);
  57. for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  58. for (int i = 1; i <= n; i++) {
  59. sum[i] = sum[i - 1] + (i & 1 ? arr[i] : -arr[i]);
  60. }
  61. bool f = false;
  62. for (int i = n; i > 0; i--) {
  63. hash.insert(sum[i]);
  64. if (f) break;
  65. if (i & 1) {
  66. if (hash.find(sum[i - 1] + k)) {
  67. f = true;
  68. break;
  69. }
  70. } else {
  71. if (hash.find(sum[i - 1] - k)) {
  72. f = true;
  73. break;
  74. }
  75. }
  76. }
  77. printf("Case #%d: %s\n", c++, f ? "Yes." : "No.");
  78. }
  79. return 0;
  80. }

hdu 5183 Negative and Positive (NP)的更多相关文章

  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) 前缀和+哈希

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

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

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

  5. HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)

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

  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. [HDOJ 5183] Negative and Positive (NP) 【Hash】

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

  9. hdu 5183(hash)

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

随机推荐

  1. USACO Section 2.3 奶牛家谱 Cow Pedigrees

    OJ:http://www.luogu.org/problem/show?pid=1472 #include<iostream> using namespace std; const in ...

  2. Oracle 自定义函数Function

    示例代码: CREATE OR REPLACE  FUNCTION "MY_DATABASE"."F_GET_USER_COUNT_BY_DEPART" ( D ...

  3. 洛谷P1466 集合 Subset Sums

    P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...

  4. Nodejs笔记(二)

    Nodejs事件 Node.js 所有的异步I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一 ...

  5. UITableView 详解 ()

    (原本取至D了个L微信公众号) UITableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRec ...

  6. 第四次java实验报告

    20145306 实验四 java 开发基础 设计过程: 1.创建项目 2.选择activity_main.xml 3.显示自己的学号 4.双击改变字体大小 5.预览

  7. Java语法细节(2)

    1.逻辑运算符 &和&&,|和||的区别 &&:和&的结果是一样的,但运算过程有区别 &&:只要左边结果为假,就不再执行右边的,结果为假 ...

  8. javaSE第三天

    第三天    12 1:运算符(掌握)    12 (1)算术运算符    12 (2)赋值运算符    12 (3)比较运算符    13 (4)逻辑运算符    13 (5)位运算符(了解)    ...

  9. 数据库mysql的基本命令

    问题分析 当数据量很大的时候,所有数据都集中在一个文本文件中的话,读写会很困难,内存消耗大,速度很慢 操作很麻烦,因为读写都要根据指定的格式尽心解析,不通用 每次获取数据都要全部数据重新读写,不能通过 ...

  10. 如何找出component的注册路径

      SELECT DISTINCT REVERSE(LTRIM(SYS_CONNECT_BY_PATH(REVERSE(PORTAL_LABEL),                           ...