Advertisement
tien_noob

Array Description ( CSES )

Feb 21st, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <cmath>
  6. #include <climits>
  7. #include <queue>
  8. using namespace std;
  9. const int N = 1e5, mod = 1e9 + 7;
  10. int dp[N][101], a[N+1], n, m, ans = 0;
  11. void read()
  12. {
  13.    cin >> n >> m;
  14.    for (int i = 1; i <= n; ++ i)
  15.    {
  16.        cin >> a[i];
  17.    }
  18. }
  19. void solve()
  20. {
  21.     if (a[1] == 0)
  22.     {
  23.         for (int i = 1; i <= m; ++ i)
  24.         {
  25.             dp[1][i] = 1;
  26.         }
  27.     }
  28.     else
  29.     {
  30.         dp[1][a[1]] = 1;
  31.     }
  32.     for (int i = 2; i <= n; ++ i)
  33.     {
  34.         if (a[i] == 0)
  35.         {
  36.             for (int j = 1; j <= m; ++ j)
  37.             {
  38.                 for (int k : {j - 1, j, j + 1})
  39.                     {
  40.                         if (k >= 1 && k <= m)
  41.                         {
  42.                             dp[i][j] = (dp[i][j] + dp[i-1][k]) % mod;
  43.                         }
  44.                     }
  45.             }
  46.         }
  47.         else
  48.         {
  49.             for (int k : {a[i] - 1, a[i], a[i] + 1})
  50.                 {
  51.                     if (k >= 1 && k <= m)
  52.                     {
  53.                         dp[i][a[i]] = (dp[i][a[i]] + dp[i-1][k]) % mod;;
  54.                     }
  55.                 }
  56.         }
  57.     }
  58.     for (int i = 1; i <= m; ++ i)
  59.     {
  60.         ans = (ans + dp[n][i]) % mod;
  61.     }
  62.     cout << ans;
  63. }
  64. int main()
  65. {
  66.     ios_base::sync_with_stdio(false);
  67.     cin.tie(nullptr);
  68.     read();
  69.     solve();
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement