题目链接: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. verview of Spring Framework--转

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html 2. Introduct ...

  2. 【转】正确使用Block避免Cycle Retain和Crash

    原文地址:http://tanqisen.github.io/blog/2013/04/19/gcd-block-cycle-retain/ 使用指南:http://blog.csdn.net/nic ...

  3. java 中能否使用 动态加载的类(Class.forName) 来做类型转换?

    今天同事提出了一个问题: 将对象a 转化为类型b,b 的classpath 是在配置文件中配置的,需要在运行中使用Class.forName 动态load进来,因为之前从来没有想过类似的问题,所以懵掉 ...

  4. WebService学习笔记系列(三)

    网上有一些提供webservice服务的网站,我们要怎么调用呢? 今天来看个如何调用手机归属地查询服务.这个网站上提供了许多webservice服务,其中包括手机归属地查询服务,我们今天就用wsimp ...

  5. 通过开发工具发布web应用到tomcat服务器中--对于小白,大神可以忽略不看,勿喷,谢谢

    需要的工具 MyEclipse和TomCat 本人用的是MyEclipse2014和TomCat7 TomCat结构图 第一步:在MyEclipse中配置TomCat 如图所示: 第二步:创建Web项 ...

  6. android 6.0获取 WRITE_SETTINGS 权限

    android 6.0上只写在AndroidManifest中是不行的,还必须手动打开才行 private void setBrightnessMode(Context context, int mo ...

  7. Google Map API v2 步步为营 (二)----- Location

    接上篇. 改造一下MapsActivity: public class MapsActivity extends Activity implements LocationListener, InfoW ...

  8. JS2 for应用

    for应用  再谈js获取元素一二: var oUl=document.getElementById('list');      //静态方法 var oUl=document.getElements ...

  9. mysql命令使用

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  10. PHP一个最简单的CMS内容管理系统

    博客是一般程序员的入手戏,写得好写不好,有没有兴趣,逻辑性够不够都从这个里面入手 我现在摒弃前台.重点讲解下如何开发一个简单的CMS系统所需要的步骤: 1.清楚流程 1--------登录后台 2-- ...