题意:
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; }