Test instructions: Ask if there are number pairs (i,j) (0<=i<=j<n), so a[i]-a[i+1]+...+ ( -1) ^ (j-i) A[j] is K.
Solution: Two methods, a raised point or an enumeration of end points.
First save the prefix and: a1-a2+a3....+/-an
The piece lifting Point method : Set the starting point to X, is actually enumerates the x-1, in two kinds of situations:
1. Start x is odd, then see if there is a[j]-a[x-1] = K, that is, a[j] = a[x-1]+k. Because the AI number in odd position is positive.
2. The beginning x is even, so see if there is a[j]-(-k) = a[x-1], i.e. a[j] = a[x-1]-k. Because the even-position AI number is negative, that is, the number of X to J is negative, and the X-to-J is selected as the starting point, so the middle is actually-K.
Mark Sum[i] as having occurred each time.
Only one hashmap is required.
Because enumerating to a starting point x, you need to determine whether A[j] (j>x) appears, so you want to reverse the enumeration.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineMod 1000000007#defineLLL __int64#definell Long Longusing namespacestd;#defineN 1000007lll Sum[n];ConstUnsignedLong LongSmod=1000007;structhashmap{structEdge {Long Longnum; intNext; }; Edge edge[2*N]; intCountedge; intHead[smod+ -]; voidinit () {memset (head,-1,sizeof(head)); Countedge=0; } voidAddedge (Long Longnum) { intstart=num%Smod; Edge[countedge].next=Head[start]; Edge[countedge].num=num; Head[start]=Countedge; Countedge++; } intFind (Long Longnum) { intstart=num%Smod; intIND; for(Ind=head[start]; ind!=-1; Ind=Edge[ind].next) { if(Edge[ind].num==num) Break; } returnIND; }}st;intMain () {intN,i,j,cs =1, T,x,k; scanf ("%d",&t); while(t--) {st.init (); scanf ("%d%d",&n,&j); sum[0] =0; for(i=1; i<=n;i++) {scanf ("%d",&x); if(i%2) Sum[i] = sum[i-1] +x; ElseSum[i] = sum[i-1] -x; } st.addedge (Sum[n]); intTag =0; for(i=n-1; i>=0; i--) { if(i%2==0&& ST. Find (sum[i]+k)! =-1) {tag =1; Break; } if(i%2&& ST. Find (sum[i]-k)! =-1) {tag =1; Break; } St.addedge (Sum[i]); } printf ("Case #%d:", cs++); if(tag) puts ("Yes."); ElsePuts"No."); } return 0;}
View Code
Enumeration Endpoint Method :
Set up two HashMap, one record sum[1],sum[3],... sum[2*cnt+1] (2*cnt+1<=n) If the odd position has occurred, and the other records whether the sum value of the even position has occurred.
Enumeration endpoint Y, the starting point may be any one of the 1~y (here subscript from the title of the 0~n-1 to 1~n), when the starting point x=1, then Np-sum (x, y) = Sum[y], recorded as XX. Take n=4 as an example.
Then the starting point is 2, the entire value is equal to-XX+A1, (-(A1-A2+A3-A4) +a1 = a2-a3+a4))
When the starting point is 3 the entire value equals xx-sum[2] (A1-A2+A3-A4-(A1-A2) = a3-a4)
... And so on, grouped into two categories:
1. xx-sum[0], xx-sum[2], ... Xx-sum[even] is K
2.-xx+sum[1],-xx+sum[3], ...-xx+sum[odd] is K
Set them to K, then that is to judge Xx-k in an even number of hashmap have not appeared, judging xx+k in the odd HashMap has not appeared.
Add sum[i] to the corresponding HashMap each time.
Sequential enumeration.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineMod 1000000007#defineLLL __int64#definell Long Longusing namespacestd;#defineN 1000007ConstUnsignedLong LongSmod=1000007;structhashmap{structEdge {Long Longnum; intNext; }; Edge edge[2*N]; intCountedge; intHead[smod+ -]; voidinit () {memset (head,-1,sizeof(head)); Countedge=0; } voidAddedge (Long Longnum) { intstart=num%Smod; Edge[countedge].next=Head[start]; Edge[countedge].num=num; Head[start]=Countedge; Countedge++; } intFind (Long Longnum) { intstart=num%Smod; intIND; for(Ind=head[start]; ind!=-1; Ind=Edge[ind].next) { if(Edge[ind].num==num) Break; } returnIND; }}mpe,mpo;intMain () {intN,i,j,cs =1, T,x,k; scanf ("%d",&t); for(cs=1; cs<=t;cs++) {mpo.init (); Mpe.init (); scanf ("%d%d",&n,&K); LLL Sum=0; Mpe.addedge (0); intTag =0; for(i=1; i<=n;i++) {scanf ("%d",&x); if(i&1) Sum + =x; ElseSum-=x; if(i&1) Mpo.addedge (sum); ElseMpe.addedge (sum); if(MPE. Find (sum-k)! =-1) {tag =1; } if(MPO.) Find (sum+k)! =-1) {tag =1; } } printf ("Case #%d:", CS); if(tag) puts ("Yes."); ElsePuts"No."); } return 0;}
View Code
Attention:
If the smod in HashMap is defined in a macro way, it will be T, with a const unsigned long long. I don't know why.
The HashMap template draws on the LOVE_DN code.
HDU 5183 Negative and Positive (NP)--hashmap