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$. 注 ...
随机推荐
- CodeForce-810B Summer sell-off (结构体排序)
http://codeforces.com/problemset/problem/810/B 已知n天里,已知第i天的供货量和需求量,给定一个f,可以在n天之中选f天促销使得供货量翻倍. 问选择其中f ...
- Stage 1 项目需求分析报告
迷你商城后台管理系统-- 需求分析 1. 引言 作为互联网热潮的崛起,消费者们的普遍差异化,实体商城要想在互联网的浪潮中继续发展,就需要制定出针对用户以及消费者的消费习惯以及喜爱品种的消费方案.从而企 ...
- PHP中的输出缓冲控制
在 PHP 中,我们直接进行 echo . 或者 print_r 的时候,输出的内容就会直接打印出来.但是,在某些情况下,我们并不想直接打印,这个时候就可以使用输出缓冲控制来进行输出打印的控制.当然, ...
- 远程连接centos7中mysql8.0
远程连接centos7中mysql8.0 1.使用Navicat for MySQL或者其它数据连接软件 2.先检查centos中防火墙是否关闭,如果关闭不需要设置,如果没有关闭防火墙,请打开3306 ...
- django class类即视图类添加装饰器的几种方法
根据别人发布整理,个人爱好收集(原文:https://blog.csdn.net/mydistance/article/details/83958655 ) 第一种:定义函数装饰器,在函数,类中使用函 ...
- 定要过python二级选择题二套
1. 2. 3. 4. 5. 6. 7. 8. 非主属性???? 9. 10.. 11.` 调用只是调用,但是没有返回值,,应为里面没有输出 12. 13. 14. 15. 16. 18. 19.
- 用Python基本库实现进度条
用Python基本库实现进度条效果几个要点:1. \r,重置光标2. time.perf_counter,计算运行时间3. 用format控制输出格式 1 #progress bar2 2 #The ...
- P2490-[SDOI2011]黑白棋【博弈论,dp】
正题 题目链接:https://www.luogu.com.cn/problem/P2490 题目大意 一个长度为\(n\)的棋盘上放下\(k\)个棋子. 第一个要是白色,下一个要是黑色,在下一个是白 ...
- P6076-[JSOI2015]染色问题【组合数学,容斥】
正题 题目链接:https://www.luogu.com.cn/problem/P6076 题目大意 给出\(n*m\)的网格,\(c\)种颜色涂色要求 每个格子可以染色也可以不染 每一行每一列至少 ...
- YbtOJ#652-集合比较【Treap】
正题 题目链接:http://www.ybtoj.com.cn/problem/652 题目大意 定义一个元素为一个有序集合包含两个元素\(C=\{A,B\}\) 集合\(C=\{A,B\}\)的大小 ...