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 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】)的更多相关文章
- 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) (手写哈希)
题目链接: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) 前缀和+哈希
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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 (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- 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 (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- [HDOJ 5183] Negative and Positive (NP) 【Hash】
题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...
- hdu5183Negative and Positive (NP))——手写Hash&&模板
题意:问是否存在一段区间其加减交错和为K. 显然,我们可以用set保存前缀和,然后枚举一个端点查找.具体的 若在st1中查找 $t$,为 $sum-t=-k$,在st2中则是 $sum-t=k$. 注 ...
随机推荐
- error: subscripted value is neither array nor pointer问题解决
在运行程序的时候报错:error: subscripted value is neither array nor pointer 原因分析:下标值不符合数组或指针要求,即操作的对象不允许有下标值. 出 ...
- dede新增字段调用方法
各位在使用{dede:channel }标签的时候,难免会遇到因为现在字段不能满足业务需求,需要新增的情况(具体怎么新增字段自行百度). 但是新增的字段通过DEDE的标签是不能直接使用的,现在博主介绍 ...
- mybatis关系表
<select id="selectSingleQuestion" resultType="remarkPaper"> select FrontTi ...
- django setting.py配置文件解读-02
定义项目目录常量 import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR ...
- python二级 之 第 五套
1. 这里要注意输入的 就是列表 . [1,2,3] 2. 就是你要明白 random.seed() 产生随机种子# 与random.randint() 取 ...
- CF183D-T-shirtx【dp,贪心】
正题 题目链接:https://www.luogu.com.cn/problem/CF183D 题目大意 \(n\)个人,\(m\)种衣服,给出每个人喜欢某件衣服的概率,你可以选择\(n\)件衣服带过 ...
- P3980-[NOI2008]志愿者招募【费用流】
正题 题目链接:https://www.luogu.com.cn/problem/P3980 题目大意 \(n\)天,第\(i\)天需要\(A_i\)个志愿者.有\(m\)种志愿者,第\(i\)种从\ ...
- bzoj4589-Hard Nim【FWT】
正题 题目链接:https://darkbzoj.tk/problem/4589 题目大意 求有多少个长度为\(n\)的数列满足它们都是不大于\(m\)的质数且异或和为\(0\). 解题思路 两个初始 ...
- P4428-[BJOI2018]二进制【树状数组,set】
正题 题目链接:https://www.luogu.com.cn/problem/P4428 题目大意 长度为\(n\)的\(0/1\)串要求支持 修改一个位置 求区间\([l,r]\)有多少个子区间 ...
- 推荐一款 Python 微服务框架 - Nameko
1. 前言 大家好,我是安果! 考虑到 Python 性能及效率性,Python Web 端一直不温不火,JAVA 和 Golang 的微服务生态一直很繁荣,也被广泛用于企业级应用开发当中 本篇文章 ...