题目描述
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers from 1 to 200. 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≤a2,
an≤an−1 and
ai≤max(ai−1,ai+1) for all i from 2 to n−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 1 to 200. Since the number of ways can be big, print it modulo 998244353.
输入
First line of input contains one integer n (2≤n≤105) — size of the array.
Second line of input contains n integers ai — elements of array. Either ai=−1 or 1≤ai≤200. ai=−1 means that i-th element can't be read.
输出
Print number of ways to restore the array modulo 998244353.
样例输入
复制样例数据
3 1 -1 2
样例输出
参考:https://www.luogu.org/problemnew/solution/CF1067A
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int modp=998244353;
const int maxn=1e6+50;
const double eps=1e-6;
#define lowbit(x) x&(-x)
#define INF 0x3f3f3f3f
inline ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int dcmp(double x)
{
if(fabs(x)<eps)return 0;
return (x>0)?1:-1;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
ll qmod(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=(ans*a)%mod;
}
b>>=1;
a=(a*a)%mod;
}
return ans;
}
ll a[maxn],dp[2][205][3];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
a[i]=read();
}
int k=0;
if(a[1]==-1){
for(int i=1;i<=200;i++){
dp[k][i][0]=1;
}
}
else{
dp[k][a[1]][0]=1;
}
ll sum=0;
for(int i=2;i<=n;i++){
//1
sum=0;
for(int j=1;j<=200;j++){
if(a[i]==-1||a[i]==j){
dp[k^1][j][0]=sum;
}
else{
dp[k^1][j][0]=0;
}
sum+=(dp[k][j][0]+dp[k][j][1]+dp[k][j][2])%modp;
sum%=modp;
}
//2
for(int j=1;j<=200;j++){
if(a[i]==-1||a[i]==j){
dp[k^1][j][1]=(dp[k][j][0]+dp[k][j][1]+dp[k][j][2])%modp;
}
else{
dp[k^1][j][1]=0;
}
}
//3
sum=0;
for(int j=200;j>=1;j--){
if(a[i]==-1||a[i]==j){
dp[k^1][j][2]=sum;
}
else{
dp[k^1][j][2]=0;
}
sum+=(dp[k][j][1]+dp[k][j][2])%modp;
sum%=modp;
}
k^=1;
}
ll ans=0;
for(int i=1;i<=200;i++){
ans+=(dp[k][i][1]+dp[k][i][2])%modp;
ans%=modp;
}
printf("%lld\n",ans);
return 0;
}