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. MATLAB仿真总结

    MATLAB仿真过程中,编写MATLAB代码的时候犯了很多错误,做了很多蠢事.记录下自己犯错的点点滴滴,并引以为戒.使用MATLAB版本为2014a,以下内容如有不当还请指正. 1. 仿真开始前清理工 ...

  2. 记录我学github的路程(二)

    2015-12-09 更新 1,现在,本地有了一个库,你可能会想到GitHub创建一个库,并且关联起来.这样,远程的库既可以当作备份,又可以让其他人通过该仓库来协作. 2,步骤: (1)登录GitHu ...

  3. 编写高质量代码改善C#程序的157个建议[勿选List<T>做基类、迭代器是只读的、慎用集合可写属性]

    前言 本文已更新至http://www.cnblogs.com/aehyok/p/3624579.html .本文主要学习记录以下内容: 建议23.避免将List<T>作为自定义集合类的基 ...

  4. AngularJS开发指南12:AngularJS的模板,CSS,数据绑定详解

    模板 AngularJS模板是一种声明式的规则.它包含了模型和控制器的信息,最后会被渲染成用户在浏览器中看到的视图.它是静态的DOM,包含HTML,CSS和AngularJS指定的元素和属性.Angu ...

  5. js回掉页面后台代码-简单demo

    后台代码: public partial class WebForm1 : System.Web.UI.Page, ICallbackEventHandler { protected void Pag ...

  6. grid-css

    .fil-container { width: 100%; max-width: 75rem; margin-right: auto; margin-left: auto; padding-left: ...

  7. asp.net 捕获全局未处理异常的几种方法

    通过HttpModule来捕获未处理的异常[推荐] 首先需要定义一个HttpModule,并监听未处理异常,代码如下: public void Init(HttpApplication context ...

  8. Java-try-catch-finally

    try-catch语句还可以包括第三部分,就是finally子句.它表示无论是否出现异常,都应当执行的内容.try-catch-finally语句的一般语法形式为: try { // 可能会发生异常的 ...

  9. ES6的模块、构建工具及应用的发布

    作者:寸志链接:https://zhuanlan.zhihu.com/p/19569085来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 总的说来就是按照将来的标准书写 ...

  10. 【poj1182】 食物链

    http://poj.org/problem?id=1182 (题目链接) 题意 中文题 Solution 带权并查集. 神犇博客,秒懂 fa记录父亲,r记录与父亲的关系.%3运用的很巧妙. 代码 / ...