Negative and Positive (NP)

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2177    Accepted Submission(s): 556

Problem Description
When given an array (a0,a1,a2,⋯an−1) and an integer K, you are expected to judge whether there is a pair (i,j)(0≤i≤j<n) which makes that NP−sum(i,j) equals to K true. Here NP−sum(i,j)=ai−ai+1+ai+2+⋯+(−1)j−iaj
 
Input
Multi test cases. In the first line of the input file there is an integer T indicates the number of test cases.
In the next 2∗T lines, it will list the data for each test case.
Each case occupies two lines, the first line contain two integers n and K which are mentioned above.
The second line contain (a0,a1,a2,⋯an−1)separated by exact one space.
[Technical Specification]
All input items are integers.
0<T≤25,1≤n≤1000000,−1000000000≤ai≤1000000000,−1000000000≤K≤1000000000
 
Output
For each case,the output should occupies exactly one line. The output format is Case #id: ans, here id is the data number starting from 1; ans is “Yes.” or “No.” (without quote) according to whether you can find (i,j) which makes PN−sum(i,j) equals to K.
See the sample for more details.
 
Sample Input
2
1 1
1
2 1
-1 0
 
Sample Output
Case #1: Yes.
Case #2: No.

Hint

If input is huge, fast IO method is recommended.

 
Source

 #include<stdio.h>
#include<string.h>
typedef long long ll ;
const int mod = + ;
int a[mod] ;
ll sum[mod] ;
int n , k ; struct edge
{
int nxt ;
int node ;
}e[mod];
int head[mod] , top ; void init ()
{
memset (head , , sizeof(head)) ;
top = ;
} void insert (ll x)
{
int y = x % mod ;
if (y < )
y += mod ;
e[++top].nxt = head[y] ;
e[top].node = x ;
head[y] = top ;
} bool find (ll x)
{
int y = x % mod ;
if (y < )
y += mod ;
for (int i = head[y] ; i ; i = e[i].nxt) {
if (e[i].node == x)
return true ;
}
return false ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
int T ;
scanf ("%d" , &T) ;
int ans = ;
while (T--) {
scanf ("%d%d" , &n , &k) ;
for (int i = ; i <= n ; i++) {
scanf ("%d" , &a[i]) ;
}
sum[] = ;
for (int i = ; i <= n ; i++) {
if (i & )
sum[i] = sum[i - ] + a[i] ;
else
sum[i] = sum[i - ] - a[i] ;
}
init () ;
bool flag = ;
for (int i = n ; i > && !flag ; i--) {
insert (sum[i]) ;
ll w ;
if (i & )
w = sum[i - ] + k ;
else
w = sum[i - ] - k ;
if (find (w))
flag = true ;
}
if (flag)
printf ("Case #%d: Yes.\n" , ++ans ) ;
else
printf ("Case #%d: No.\n" , ++ans ) ;
}
return ;
}

583ms

 第一次遇到哈希表,它能把查找一个数的复杂度降到0(1) 。
我学会的那种写法是通过“ 前向星 ”实现的,
他通过对插入数取余把数字存到数组中,从而防止了carsh , nxt记录的是上一个和当前输入的数 取余 后相等的数 在 数组中的下标。
这道题思路:
sum[i] = a0 - a1…… (-1)^n*an ;
将他们存入哈希表中
然后从n~1寻找哈希表中是否有sum[i] + k

ps:另外用lower_bound + sort也能办到

hdu 5183. Negative and Positive (哈希表)的更多相关文章

  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) 前缀和+哈希

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

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

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

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

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

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

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

  8. [HDOJ 5183] Negative and Positive (NP) 【Hash】

    题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...

  9. hdu acm 1425 sort(哈希表思想)

    sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 4.HBase In Action 第一章-HBase简介(1.1.2 数据创新)

    As we now know, many prominent internet companies, most notably Google, Amazon, Yahoo!, and Facebook ...

  2. FPGA学习之基本结构

    如何学习FPGA中提到第一步:学习.了解FPGA结构,FPGA到底是什么东西,芯片里面有什么,不要开始就拿个开发板照着别人的东西去编程.既然要开始学习FPGA,那么就应该从其基本结构开始.以下内容是我 ...

  3. ArrayList List<T> T[] Array

    ArrayList    其实就是一个存储obj列表的类 ArrayList 接受 null 作为有效值并且允许重复的元素. 不保证会对 ArrayList 排序. 在执行需要对 ArrayList ...

  4. [poj2184]我是来水一下背包的

    http://poj.org/problem?id=2184 题意:01背包的变种,就是说有2组值(有负的),你要取一些物品是2阻值的和非负且最大 分析: 1.对于负的很好处理,可以把他们都加上一个数 ...

  5. 浅谈分治算法在OI中的应用

    分治虽然是基本思想,但是OI中不会出裸分治让你一眼看出来,往往都是结合到找规律里面. 先来个简单的: 奇妙变换 (magic.pas/c/cpp) [问题描述]   为了奖励牛牛同学帮妈妈解决了大写中 ...

  6. shell中的正则表达式

    1.正则与通配符 linux中的通配符是用来匹配文件名的,其匹配是完全匹配.只支持通配符则命令有ls find cp等命令 正则是用来匹配字符串的,是包含匹配.只要搜索的内容在某个字符串中,那么改字符 ...

  7. Java设计模式-责任链模式(Chain of Responsibility)

    接下来我们将要谈谈责任链模式,有多个对象,每个对象持有对下一个对象的引用,这样就会形成一条链,请求在这条链上传递,直到某一对象决定处理该请求.但是发出者并不清楚到底最终那个对象会处理该请求,所以,责任 ...

  8. 深入研究Struts2(一)---Struts2是什么?它的工作原理是什么?

    本文绝对原创, 欢迎转载, 但是转载请记得注明文章出处:http://blog.csdn.net/izard999/article/details/39891281 近4年都在从事Android方 面 ...

  9. 【codevs1044】导弹拦截问题与Dilworth定理

    题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某 ...

  10. GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决

    直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...