Topics
summary of http://172.16.0.132/senior/#contest/SHOW/2061/3
This question is actually DP.
We set F[I,J] to indicate the number of scenarios where the height of the first I is J.
So we can know, f[i,j]=f[i-1,j-1]+f[i-1,j]+f[i-1,j+1]; but we're going to enumerate the J, so we'll see how much J can enumerate. We know that each point is either 1 more than the previous one, or 1 less than the previous one. So we can preprocess all the highest height, for example, n=8, then we know h[1] and h[n] are equal to 0, so n=8 is 0,1,2,3,3,2,1,0. Finally, we can do this very easily, but it will be t.
We considered optimization because all of it was mod 1000000007, but it was very slow, so we thought about the MoD every 20 times so we could optimize the time. Just A.
var n,m,i,k,j,l1,l2:longint;
H,max:array[0..20000]of Longint;
F:array[0..1,-1..20001]of Int64;
Begin assign (input, ' brick.in '); reset (input);
Assign (output, ' brick.out '); rewrite (output);
READLN (n);
For I:=1 to n do read (H[i]);
If n mod 2=0 then the begin for i:=2 to N Div 2 do max[i]:=max[i-1]+1;
Max[n Div 2+1]:=max[n Div 2];
For I:=n Div--------n do max[i]:=max[i-1]-1;
End ELSE begin for i:=2-N div 2+1 do max[i]:=max[i-1]+1;
For I:=n Div--------n do max[i]:=max[i-1]-1;
End
If H[1]>0 then begin Writeln (0);
Halt
End
F[1,0]:=1;
h[1]:=0;
h[n]:=0;
L1:=1; For i:=2 to n do begin K:= 0;
If I mod 20=0 then k:=1;
L1:=i MoD 2;
l2:= (i-1) mod 2;
If H[i]=-1 then begin for j:=0 to Max[i] do begin
If K=1 then f[l1,j]:= (f[l2,j-1]+f[l2,j]+f[l2,j+1]) mod 1000000007
else f[l1,j]:=f[l2,j-1]+f[l2,j]+f[l2,j+1];
End
End else begin j:=h[i]; If K=1 then f[l1,j]:= (f[l2,j-1]+f[l2,j]+f[l2,j+1]) mod 1000000007 else f[l1,j]:=f[l2,j-1]+
F[L2,J]+F[L2,J+1];
End
Fillchar (F[l2],sizeof (f[l2]), 0);
End
Writeln (f[l1,0] mod 1000000007);
Close (input);
Close (output); End.