题意:

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了,,,,,

代码:

  1. set<ll> st;
  2. ll ans,K;
  3. int n;
  4. int T;
  5. ll a[1000005];
  6.  
  7. int main(){
  8.  
  9. cin>>T;
  10. rep(t,1,T){
  11. scanf("%d%I64d",&n,&K);
  12. rep(i,1,n){
  13. scanf("%I64d",&a[i]);
  14. }
  15. printf("Case #%d: ",t);
  16.  
  17. ans=0;
  18. st.clear();
  19. st.insert(0);
  20. int td=1;
  21. rep(i,1,n){
  22. ans+=(a[i]*td);
  23. if(st.find(ans-K)!=st.end()){
  24. puts("Yes.");
  25. goto NEXT;
  26. }
  27. if(td==-1){
  28. st.insert(ans);
  29. }
  30. td=-td;
  31. }
  32.  
  33. st.clear();
  34. ans=0;
  35. td=-1;
  36. rep(i,1,n){
  37. ans+=(a[i]*td);
  38. if(st.find(ans-K)!=st.end()){
  39. puts("Yes.");
  40. goto NEXT;
  41. }
  42. if(td==-1){
  43. st.insert(ans);
  44. }
  45. td=-td;
  46. }
  47. puts("No.");
  48.  
  49. NEXT:;
  50. }
  51.  
  52. return 0;
  53. }

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. CodeForce-810B Summer sell-off (结构体排序)

    http://codeforces.com/problemset/problem/810/B 已知n天里,已知第i天的供货量和需求量,给定一个f,可以在n天之中选f天促销使得供货量翻倍. 问选择其中f ...

  2. Stage 1 项目需求分析报告

    迷你商城后台管理系统-- 需求分析 1. 引言 作为互联网热潮的崛起,消费者们的普遍差异化,实体商城要想在互联网的浪潮中继续发展,就需要制定出针对用户以及消费者的消费习惯以及喜爱品种的消费方案.从而企 ...

  3. PHP中的输出缓冲控制

    在 PHP 中,我们直接进行 echo . 或者 print_r 的时候,输出的内容就会直接打印出来.但是,在某些情况下,我们并不想直接打印,这个时候就可以使用输出缓冲控制来进行输出打印的控制.当然, ...

  4. 远程连接centos7中mysql8.0

    远程连接centos7中mysql8.0 1.使用Navicat for MySQL或者其它数据连接软件 2.先检查centos中防火墙是否关闭,如果关闭不需要设置,如果没有关闭防火墙,请打开3306 ...

  5. django class类即视图类添加装饰器的几种方法

    根据别人发布整理,个人爱好收集(原文:https://blog.csdn.net/mydistance/article/details/83958655 ) 第一种:定义函数装饰器,在函数,类中使用函 ...

  6. 定要过python二级选择题二套

    1. 2. 3. 4. 5. 6. 7. 8. 非主属性???? 9. 10.. 11.` 调用只是调用,但是没有返回值,,应为里面没有输出 12. 13. 14. 15. 16. 18. 19.

  7. 用Python基本库实现进度条

    用Python基本库实现进度条效果几个要点:1. \r,重置光标2. time.perf_counter,计算运行时间3. 用format控制输出格式 1 #progress bar2 2 #The ...

  8. P2490-[SDOI2011]黑白棋【博弈论,dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P2490 题目大意 一个长度为\(n\)的棋盘上放下\(k\)个棋子. 第一个要是白色,下一个要是黑色,在下一个是白 ...

  9. P6076-[JSOI2015]染色问题【组合数学,容斥】

    正题 题目链接:https://www.luogu.com.cn/problem/P6076 题目大意 给出\(n*m\)的网格,\(c\)种颜色涂色要求 每个格子可以染色也可以不染 每一行每一列至少 ...

  10. YbtOJ#652-集合比较【Treap】

    正题 题目链接:http://www.ybtoj.com.cn/problem/652 题目大意 定义一个元素为一个有序集合包含两个元素\(C=\{A,B\}\) 集合\(C=\{A,B\}\)的大小 ...