题目链接: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. 全文索引(三)lucene 分词 Analyzer

    分词: 将reader通过阅读对象Analyzer字处理,得到TokenStream处理流程被称为分割. 该解释可能是太晦涩.查看示例,这个东西是什么感性的认识. 样品:一段文本"this ...

  2. sort()排序 collections.sort();

    1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...

  3. Paxos算法之旅(四)zookeeper代码解析--转载

    ZooKeeper是近期比较热门的一个类Paxos实现.也是一个逐渐得到广泛应用的开源的分布式锁服务实现.被认为是Chubby的开源版,虽然具体实现有很多差异.ZooKeeper概要的介绍可以看官方文 ...

  4. Mysql解压版的安装

    Mysql解压版的安装 ——@梁WP 1.解压mysql到合适的地方 2.右击计算机-属性-高级系统设置-高级-环境变量,弹出“环境变量”对话框,修改下面的系统变量 3.新建MYSQL_HOME变量, ...

  5. virtualbox 安装windows系统的一些问题

    今天总结一下,使用virtualbox安装windows系统的一些问题. 安装的是Ghost的系统,正版系统也可以参考. 首先本人的机器原系统是ubuntu 16.04 LTS x64 1.win7或 ...

  6. Papers

    Research on Semantic Text Mining Based on Domain Ontologyhttp://link.springer.com/chapter/10.1007/97 ...

  7. 一个少了context的赋值的错误

    错误类型如下,怎么也找不到错误,后来仔细看了源代码,原来忘了context的赋值,只是声明,声明后不马上引用到值容易出事. 11-12 15:00:09.877: E/AndroidRuntime(6 ...

  8. 《将博客搬至CSDN》的文章

    我的CSDN地址 博客园应该以后会很少来了.

  9. 'EntityValidationErrors' property for more details

    很多小猿遇到这个Exception 的时候,都会有点无厘头.这个时候最好try-- catch下,找到出错的地方.本人习惯在页面上加个lable标签,把exc msg(exception messag ...

  10. 一、C# 概述

    1.托管执行环境 2.程序集 3.关键字:C#保留字 4.除了C#定义的关键字之外,开发者可以提供他们自己的名称,编程语言将这些名称称为标识符 5.如果关键字包含一个"@"前缀,那 ...