#include <bits/stdc++.h>
#define int long long
using namespace std;
int dp[1000100][3];
int ma[1000100];
vector<int>x;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >>N;
int i;
for(i=1;i<N;i++)
{
int a;
cin >>a;
x.push_back(a);
}
x.push_back(0);
dp[0][0]=1;
if(x[0]>2)
{
cout <<0;
return 0;
}
for(i=0;i<N;i++)
{
ma[i+1]=max(ma[i],x[i]);
}
for(i=1;i<=N;i++)
{
if(ma[i-1]+1>=x[i-1])
{
dp[i][0]=dp[i-1][0];
dp[i][2]+=dp[i-1][0];
dp[i][1]+=(ma[i-1]-1)*dp[i-1][0];
if(ma[i-1]+1==x[i-1])
{
dp[i][2]-=dp[i-1][0];
dp[i][1]+=dp[i-1][0];
}
}
else
{
if(ma[i-1]+2>=x[i-1])
{
dp[i][2]=dp[i-1][0];
}
else
{
cout <<0;
return 0;
}
}
if(i!=1)
{
if(ma[i-2]+1>=x[i-2])
{
dp[i][1]+=dp[i-1][1];
}
if(ma[i-2]+2>=x[i-2])
{
if(ma[i-2]+1<=ma[i-1])
dp[i][1]+=dp[i-1][2];
else
dp[i][2]+=dp[i-1][2];
}
}
}
cout <<dp[N][1]+dp[N][2]+dp[N][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
132 ms |
40636 KB |
Output is correct |
22 |
Correct |
86 ms |
37744 KB |
Output is correct |
23 |
Correct |
88 ms |
40572 KB |
Output is correct |
24 |
Correct |
100 ms |
40592 KB |
Output is correct |
25 |
Correct |
112 ms |
40540 KB |
Output is correct |
26 |
Correct |
113 ms |
40492 KB |
Output is correct |
27 |
Correct |
98 ms |
40540 KB |
Output is correct |
28 |
Correct |
114 ms |
40500 KB |
Output is correct |
29 |
Correct |
87 ms |
40588 KB |
Output is correct |
30 |
Correct |
88 ms |
40492 KB |
Output is correct |
31 |
Correct |
99 ms |
40496 KB |
Output is correct |
32 |
Correct |
114 ms |
40492 KB |
Output is correct |
33 |
Correct |
90 ms |
40592 KB |
Output is correct |
34 |
Correct |
111 ms |
40548 KB |
Output is correct |
35 |
Correct |
119 ms |
40536 KB |
Output is correct |