题目链接: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. [Redux] Filtering Redux State with React Router Params

    We will learn how adding React Router shifts the balance of responsibilities, and how the components ...

  2. 2014-08-05 pig

    Pig的数据类型能够分为两种:一种是scalar类型,包含单一的value,一种是complex类型,包含有其他的类型. 对于scalar类型: int,long,float,double,chara ...

  3. LabVIEW设计模式系列——事件结构中值改变事件

    标准:1.将具有值改变事件的控件,放置在其事件结构的值改变页面里.

  4. 基于VMware为CentOS 6.5配置两个网卡

    为CentOS 6.5配置两块网卡,一块是eth0,一块是eth1,下面以master为例 1.选择“master”-->“编辑虚拟机设置”,如下所示 2.单击“添加”,如下 3.选择“网络适配 ...

  5. Cocos2d的特性

    从本质上说,Cocos2d是一个图形引擎,封装了复杂的图形接口,通过抽象出精灵.动作等概念,降低了游戏开发难度,简化了开发过程.Cocos2d-x为保证游戏能方便地移植到不同平台上,又在此基础上做了很 ...

  6. android开发之定制ViewPager滑动事件

    明天还要加班,苦逼的程序猿,简单说说最近遇到的一个问题吧. 我在viewpager+fragment学习笔记中简单介绍过ViewPager+Fragment的用法,其实并不难,当时实现了一个如下图所示 ...

  7. Java基础知识强化之网络编程笔记07:TCP之服务器给客户端一个反馈案例

    1. 首先我们搭建服务器端的代码,如下: package cn.itcast_07; import java.io.IOException; import java.io.InputStream; i ...

  8. 各种SQL类型

    一.SQL:通常我们说的sql指的是最古老运用最方法的结构化查询语言(Structured Query Language),大部分人接触最多的是数据库查询使用,关系型数据库基本都支持. 二.T-SQL ...

  9. Gradle插件

    1.方法数统计 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.1' apply plugin: 'com.getkeep ...

  10. Asp.Net Mvc4 Ajax提交数据成功弹框后跳转页面

    1.cshtml页面代码 @model Model.UserInfo @{     ViewBag.Title = "Edit"; var options = new AjaxOp ...