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. js中模仿接口继承

    一般情况下我们会这样写,但是这样写的话,不够美化或者直观. 如果我们可以这样写的话,感觉更好: 但是样子的话,我们没有考虑原型覆盖之类的,因为我们通常的情况,我们继承只有一层,在通常情况下,我们原型覆 ...

  2. 我的bootstrap使用的历程

    1.bootstrap快速开发,和amaze一样,同样是自己布局,然后找对应的模板,然后复制. 2.bootstrap实现的不完美的地方,我们要靠自己的样式去解决. 典型的居中布局, containe ...

  3. [C#]exchange发送,收件箱操作类

    最近项目中需要用到exchange的操作,就参照msdn弄了一个简单的操作类.目前先实现了,发送邮件和拉取收件箱的功能,其他的以后在慢慢的添加. using Microsoft.Exchange.We ...

  4. CSS_复习

    //这个可以作为补白居中的替补方法<!doctype html> <html> <head> <meta charset="utf-8"& ...

  5. Java设计模式-状态模式(State)

    核心思想就是:当对象的状态改变时,同时改变其行为,很好理解!就拿QQ来说,有几种状态,在线.隐身.忙碌等,每个状态对应不同的操作,而且你的好友也能看到你的状态,所以,状态模式就两点:1.可以通过改变状 ...

  6. Tomcat server.xml详解

    Server.xml的结构大致 <Server port="8005" shutdown="SHUTDOWN"> <Service name= ...

  7. <supports-screens>的用法

    <supports-screens android:resizeable=["true"| "false"] android:smallScreens=[ ...

  8. 【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905 - Meteor

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/D The famous Korean internet co ...

  9. BIEE连接数据库的方法

    BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...

  10. BZOJ-1861 Book 书架 Splay

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1010 Solved: 588 [Submit][Stat ...