[HDOJ 5183] Negative and Positive (NP) 【Hash】
题目链接: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】的更多相关文章
- hdu 5183 Negative and Positive (NP)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...
- 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 ...
- HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- HDU 5183 Negative and Positive (NP) 前缀和+哈希
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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\ ...
- 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 ...
- 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 ...
- hdu 5183. Negative and Positive (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- android开发之Toast的多种应用
Toast最基本的功能就是弹出一个弱提示,这个很简单我就不说了,说说Toast一些其他的作用. 来公司的时候,公司产品的1.0版本已经发布出去了,但是1.0是一个必须联网才能使用的产品,在2.0中想让 ...
- Java-Android 之Hello World
1.新建一个Android Project 2.2版本的 修改values下面的内容,为: <?xml version="1.0" encoding="utf-8& ...
- webrtc学习———记录一
最近导师让研究一下webrtc,希望将来用到我们的ICT2系统中. 但是从来没有过做web的基础,无论前端还是后端,html.js全都从头学起.html还好说,没有太过复杂的东西. js就有点难度了, ...
- sql中对查询出来的数据进行分页
当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...
- 分享:Svg文件转换为图片(调用 Inkscape 命令行)
其实只是做了简单封装,可以方便进行批量转换. 获取Svg对象坐标的代码请看:根据svg节点对象类型和路径值转换坐标值, DrawingColor方法是进行颜色填充的. /// <summary& ...
- EL标签和JSTL标签---JSP页面的应用
====EL(Expression Language)表达式语言:用于计算和输出存储在标志位置(page.request.session.application)的java对象的值: 1.开启和关闭E ...
- 【html】【0】开始的序言
人生总得做点什么才显得有意义,在牛逼的梦想也抵挡不住你傻逼似的坚持! 1>本系列适用于没有任何计算机语言基础的小白入门级教程 2>为了我喜欢的一个女生小娜娜 3>为自己系统的学习ht ...
- WCF大文件传输服务
由于项目需要,自己写一个基于WCF的大文件传输服务雏形.觉得有一定的参考价值,因此放在网上分享. 目前版本为v1.1特点如下: 1.文件传输端口为18650 2.上传和下载文件 3.支持获取文件传输状 ...
- Codeforces 549C The Game Of Parity(博弈)
The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...
- Eclipse Git和sourceTree用法
Eclipse Git和sourceTree用法 Eclipse Git: 提交代码到git: 1.team->Repository->pull 若没有冲突: 2.team->com ...