题意:

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

1≤n≤1000000,−1000000000≤ai≤1000000000,−1000000000≤K≤1000000000

思路:

原先想是不是要用树状数组,,,,,,

其实只要线性扫,然后判断sum[i]-K在之前是不是出现过即可。

用map会超时,用set险过。。【set效率比map高?】

*:时时候整理一套自己的HASH了,,,,,

代码:

set<ll> st;
ll ans,K;
int n;
int T;
ll a[1000005]; int main(){ cin>>T;
rep(t,1,T){
scanf("%d%I64d",&n,&K);
rep(i,1,n){
scanf("%I64d",&a[i]);
}
printf("Case #%d: ",t); ans=0;
st.clear();
st.insert(0);
int td=1;
rep(i,1,n){
ans+=(a[i]*td);
if(st.find(ans-K)!=st.end()){
puts("Yes.");
goto NEXT;
}
if(td==-1){
st.insert(ans);
}
td=-td;
} st.clear();
ans=0;
td=-1;
rep(i,1,n){
ans+=(a[i]*td);
if(st.find(ans-K)!=st.end()){
puts("Yes.");
goto NEXT;
}
if(td==-1){
st.insert(ans);
}
td=-td;
}
puts("No."); NEXT:;
} return 0;
}

hdu 5183 Negative and Positive (NP)(STL-集合【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) (手写哈希)

    题目链接:HDU 5183 Problem Description When given an array \((a_0,a_1,a_2,⋯a_{n−1})\) and an integer \(K\ ...

  3. HDU 5183 Negative and Positive (NP) 前缀和+哈希

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

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

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

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

  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 (哈希表)

    Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

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

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

  9. hdu5183Negative and Positive (NP))——手写Hash&&模板

    题意:问是否存在一段区间其加减交错和为K. 显然,我们可以用set保存前缀和,然后枚举一个端点查找.具体的 若在st1中查找 $t$,为 $sum-t=-k$,在st2中则是 $sum-t=k$. 注 ...

随机推荐

  1. JAVA语言程序设计课程评价

    紧张的又短暂的一个学期结束了,这个学期也许将成为我人生中一个重要的转折点,作为一名半路出家的选手,在初次了解Java语言时我感到非常的迷茫与不知所措.因为之前很多同学都是通过假期时间在家自学,刚转入新 ...

  2. 自己实现一个Controller——精简型

    写在最前 controller-manager作为K8S master的其中一个组件,负责众多controller的启动和终止,这些controller负责监控着k8s中各种资源,执行调谐,使他们的实 ...

  3. javascript 中介者模式 mediator

    * player.js /** * 中介者模式 * @param {*} name 角色名称 * @param {*} teamColor 队伍颜色 */ function Player(name, ...

  4. modern php enable zend opcache

    字节码缓存能存储预先编译好的php代码 * 如果是自己编译PHP ./configure --enable-opcache 编译好后 php.ini zend_extension=opcache.so ...

  5. VMware安装最新版CentOS7图文教程

    https://blog.csdn.net/reticent_man/article/details/80732395 https://blog.csdn.net/q2158798/article/d ...

  6. redis小结 1-2

    1.Redis 发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了频道 c ...

  7. python mysql增加改查

    #python mysql增加改查,方便日后copy/paste .......... 1 #encoding: UTF-8 2 import pymysql 3 # # 打开数据库连接 4 db = ...

  8. JavaScript 获取html元素

    1.通过ID获取: document.getElementById("idname"); 2.通过class.tagname获取: var wcyclass = document. ...

  9. es相关监控指标梳理

    ###################ElasticSearch监控指标梳理########################### #author:lugh1 # #date:2021-09-26 # ...

  10. element-ui上传多个文件时会发送多个请求

    1. element-ui的默认 默认是异步多次请求上传单个文件 如果业务就是单纯的上传文件,那么这个样子是没有问题的 前端代码参考 https://element-plus.gitee.io/#/z ...