zoukankan      html  css  js  c++  java
  • hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)

    题意:

    When given an array (a0,a1,a2,an1) and an integer K, you are expected to judge whether there is a pair (i,j)(0ij<n) which makes that NPsum(i,j) equals to K true. Here NPsum(i,j)=aiai+1+ai+2++(1)jiaj

    1n1000000,1000000000ai1000000000,1000000000K1000000000

    思路:

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

    其实只要线性扫,然后判断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;
    }
  • 相关阅读:
    SQL 初级教程学习(二)
    QuartzJobs 如何发布服务
    Net 发布网站中遇到的几点问题
    PWBI--Excel 数据源
    微信小程序资源
    加密和解密之非对称加密
    Js 使用小技巧总结(1)
    路径的读取
    json和Jsonp 使用总结(3)
    json和Jsonp 使用总结(2)
  • 原文地址:https://www.cnblogs.com/fish7/p/4330831.html
Copyright © 2011-2022 走看看