题目链接:HDOJ - 5183

题目分析

分两种情况,奇数位正偶数位负或者相反。

从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中。

BestCoder比赛的时候我写了 STL map, 然后TLE...

注意: Hash负数的时候 % 了一个质数,得到的是负数还要 + Mod !!

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector> using namespace std; #define Debug(x) cout << #x << " = " << x << endl typedef long long LL;
typedef double DB; inline int gmax(int a, int b) {return a > b ? a : b;}
inline int gmin(int a, int b) {return a < b ? a : b;} inline void Read(int &Num)
{
char c = getchar();
bool Neg = false;
while (c < '0' || c > '9')
{
if (c == '-') Neg = true;
c = getchar();
}
Num = c - '0'; c = getchar();
while (c >= '0' && c <= '9')
{
Num = Num * 10 + c - '0';
c = getchar();
}
if (Neg) Num = -Num;
} const int MaxN = 1000000 + 5, Mod = 1000007; int T, n, k;
int A[MaxN]; struct HashNode
{
int x;
HashNode *Next;
} HA[MaxN], *P = HA, *Hash[2][Mod + 5]; bool Find(int f, LL Num)
{
int HN = ((Num % Mod) + Mod) % Mod;
for (HashNode *j = Hash[f][HN]; j; j = j -> Next)
if (j -> x == Num) return true;
return false;
} void Insert(int f, LL Num)
{
int HN = ((Num % Mod) + Mod) % Mod;
++P; P -> x = Num;
P -> Next = Hash[f][HN]; Hash[f][HN] = P;
} int main()
{
scanf("%d", &T);
for (int Case = 1; Case <= T; ++Case)
{
memset(Hash, 0, sizeof(Hash));
P = HA;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) Read(A[i]);
int Temp;
LL Sum0, Sum1;
Sum0 = Sum1 = 0;
Insert(1, 0);
bool Flag = false;
for (int i = 1; i <= n; ++i)
{
if (i & 1) Temp = -A[i];
else Temp = A[i];
Sum0 = Sum0 + (LL)Temp;
Sum1 = Sum1 - (LL)Temp;
if (Find(0, Sum0 - (LL)k) || Find(1, Sum1 - (LL)k))
{
Flag = true;
break;
}
if (i & 1) Insert(0, Sum0);
if ((i & 1) == 0) Insert(1, Sum1);
}
if (Flag) printf("Case #%d: Yes.\n", Case);
else printf("Case #%d: No.\n", Case);
}
return 0;
}

  

[HDOJ 5183] Negative and Positive (NP) 【Hash】的更多相关文章

  1. hdu 5183 Negative and Positive (NP)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...

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

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

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

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

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

  5. 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\ ...

  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 (NP) --Hashmap

    题意:问有没有数对(i,j)(0<=i<=j<n),使得a[i]-a[i+1]+...+(-1)^(j-i)a[j]为K. 解法:两种方法,枚举起点或者枚举终点. 先保存前缀和:a1 ...

  8. hdu 5183. Negative and Positive (哈希表)

    Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  9. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. 日志分析(php+nosql+rsync+crontable)

    是不是经常要分析用户的行为?是不是经常遇到多台server上传的日志一起分析?是不是对数据统计的间隔时间要求非常短?还有木有由于日志文件过大,而须要分块处理? 1.说明一点在日志写入的时候必须依照一种 ...

  2. [TypeScript] Reflection and Decorator Metadata

    TypeScript allows you to emit decorator metadata which enables more powerful features through reflec ...

  3. Linux Kernel 排程機制介紹

    http://loda.hala01.com/2011/12/linux-kernel-%E6%8E%92%E7%A8%8B%E6%A9%9F%E5%88%B6%E4%BB%8B%E7%B4%B9/ ...

  4. Qt学习之路: 国际化(上)

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://devbean.blog.51cto.com/448512/244689 2D ...

  5. HDU2024JAVA

    C语言合法标识符 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. 【Android】数据库的简单应用——创建数据库

    SQLiteOpenHelper是一个抽象类,要使用它必须写一个类继承它.SQLiteOpenHelper有两个抽象方法onCreate()和onUpgrade(),我们要在类里面重写这两个方法来实现 ...

  7. DOS和UNIX文本文件之间相互转换的方法

    在Unix/Linux下可以使用file命令查看文件类型,如下: file dosfile.txt 使用dos2unix 一般Linux发行版中都带有这个小工具,只能把DOS转换为UNIX文件,命令如 ...

  8. 利用jQuery npoi插件 asxh一般处理文件实现excel的下载

    最近开发的过程中遇到这么一个问题,利用ajax和ashx文件实现下载功能.发现代码调试走完之后并没有弹出下载框. 研究了一段时间之后发现解决这种问题有两种方法,1.ajax获取数据集在前台做处理实现导 ...

  9. 25、Javascript 事件

    Javascript 事件 是指 Javascript 捕获到用户的操作,并做出正确的相应. Javascript 事件一般与DOM元素绑定. Javascript处理事件的基本机制 1.对DOM元素 ...

  10. [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)

    点击下载 CsvHelper.rar 这个类是关于Csv文件的一些高级操作1.DataTable导出到CSV2.将Csv读入DataTable看下面代码吧 /// <summary> // ...