Submission #50087017


Source Code Expand

#include<bits/stdc++.h>

using namespace std;

#define mod 998244353

long long dp[5005][5005]={0};
long long tot[5005]={0};
int a[5005];
int jud[5005][5005];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  for(int i=1;i<=n;i++){
    cin >> a[i];
  }
  for(int i=1;i<=n;i++){
    int f=-1;
    for(int j=i;j<=n;j++){
      if(a[j]!=-1){
        if(f==-1){f=a[j];}
        else if(f!=a[j]){f=-2;}
      }
      jud[i][j]=f;
    }
  }

  tot[0]=1;
  if(a[1]==-1){
    for(int i=1;i<=n;i++){dp[1][i]=1;}
    tot[1]=n;
  }
  else{
    dp[1][a[1]]=1;
    tot[1]=1;
  }

  for(int i=2;i<=n;i++){
    if(a[i]==-1){
      long long whole=0;
      for(int j=1;j<=n;j++){
        whole+=dp[i-1][j];
        whole%=mod;
      }
      for(int j=1;j<=n;j++){
        dp[i][j]=whole;
      }

      // treat prev == a[i]
      for(int j=1;j<=n;j++){
        int tg=i-j;
        if(tg>=1 && (jud[tg][i]==-1 || jud[tg][i]==j)){
          // cout << i << " " << j << "\n";
          dp[i][j]+=(mod-tot[tg-1]);
          dp[i][j]%=mod;
          dp[i][j]+=dp[tg-1][j];
          dp[i][j]%=mod;
        }
      }
    }
    else{
      for(int j=1;j<=n;j++){
        dp[i][a[i]]+=dp[i-1][j];
        dp[i][a[i]]%=mod;
      }

      // treat prev == a[i]
      int j=a[i];
      int tg=i-j;
      if(tg>=1 && (jud[tg][i]==-1 || jud[tg][i]==j)){
        dp[i][j]+=(mod-tot[tg-1]);
        dp[i][j]%=mod;
        dp[i][j]+=dp[tg-1][j];
        dp[i][j]%=mod;
      }
    }

    for(int j=1;j<=n;j++){
      tot[i]+=dp[i][j];
    }
    tot[i]%=mod;
  }

  // for(int i=1;i<=n;i++){
  //   for(int j=1;j<=n;j++){cout << dp[i][j] << " ";}
  //   cout << "\n";
  // }
  cout << tot[n] << "\n";
  return 0;
}

Submission Info

Submission Time
Task C - Not So Consecutive
User physics0523
Language C++ 20 (gcc 12.2)
Score 600
Code Size 1816 Byte
Status AC
Exec Time 428 ms
Memory 265852 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 33
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 1 ms 3376 KiB
00-sample-002.txt AC 1 ms 3444 KiB
00-sample-003.txt AC 1 ms 3532 KiB
00-sample-004.txt AC 1 ms 3636 KiB
01-001.txt AC 1 ms 3340 KiB
01-002.txt AC 1 ms 3464 KiB
01-003.txt AC 1 ms 3792 KiB
01-004.txt AC 1 ms 3452 KiB
01-005.txt AC 1 ms 3320 KiB
01-006.txt AC 1 ms 3612 KiB
01-007.txt AC 1 ms 3584 KiB
01-008.txt AC 19 ms 21960 KiB
01-009.txt AC 218 ms 158268 KiB
01-010.txt AC 5 ms 9048 KiB
01-011.txt AC 318 ms 251132 KiB
01-012.txt AC 182 ms 150744 KiB
01-013.txt AC 427 ms 265852 KiB
01-014.txt AC 365 ms 265816 KiB
01-015.txt AC 346 ms 265732 KiB
01-016.txt AC 362 ms 265704 KiB
01-017.txt AC 332 ms 265628 KiB
01-018.txt AC 426 ms 265852 KiB
01-019.txt AC 428 ms 265748 KiB
01-020.txt AC 425 ms 265624 KiB
01-021.txt AC 427 ms 265756 KiB
01-022.txt AC 424 ms 265536 KiB
01-023.txt AC 328 ms 265456 KiB
01-024.txt AC 307 ms 262572 KiB
01-025.txt AC 286 ms 236936 KiB
01-026.txt AC 222 ms 159172 KiB
01-027.txt AC 155 ms 90140 KiB
01-028.txt AC 156 ms 90140 KiB
01-029.txt AC 155 ms 90344 KiB