A. Array Without Local Maximums
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of nn numbers from 11 to 200200. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:
a1≤a2a1≤a2,
an≤an−1an≤an−1 and
ai≤max(ai−1,ai+1)ai≤max(ai−1,ai+1) for all ii from 22 to n−1n−1.
Ivan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from 11 to 200200. Since the number of ways can be big, print it modulo 998244353998244353.
Input
First line of input contains one integer nn (2≤n≤1052≤n≤105) — size of the array.
Second line of input contains nn integers aiai — elements of array. Either ai=−1ai=−1 or 1≤ai≤2001≤ai≤200. ai=−1ai=−1 means that ii-th element can't be read.
Output
Print number of ways to restore the array modulo 998244353998244353.
Examples
input
Copy
3 1 -1 2
output
Copy
1
input
Copy
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 1e5 + 10;
int a[maxn];
ll dp[2][210][3];
int main()
{
int n; cin >> n;
int k = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
if (a[1] == -1)
for (int i = 1; i <= 200; i++)
dp[k][i][0] = 1;
else dp[k][a[1]][0] = 1;
for (int i = 2; i <= n; i++, k ^= 1)
{
ll s = 0;
for (int j = 1; j <= 200; j++)
{
dp[k ^ 1][j][0] = (a[i] == -1 || a[i] == j) ? s : 0;
(s += dp[k][j][0] + dp[k][j][1] + dp[k][j][2]) %= mod;
dp[k ^ 1][j][1] = (a[i] == -1 || a[i] == j) ? dp[k][j][0] + dp[k][j][1] + dp[k][j][2] : 0;
}
s = 0;
for(int j = 200; j >= 1; j--)
dp[k ^ 1][j][2] = (a[i] == -1 || a[i] == j) ? s : 0, (s += dp[k][j][1] + dp[k][j][2]) %= mod;
}
ll res = 0;
for (int i = 1; i <= 200; i++)
(res += dp[k][i][1] + dp[k][i][2]) %= mod;
cout << res << endl;
}
2 -1 -1
output
Copy
200
Note
In the first example, only possible value of a2a2 is 22.
In the second example, a1=a2a1=a2 so there are 200200 different values because all restored elements should be integers between 11 and 200200.