1 solutions

  • 0
    @ 2025-10-20 16:53:57

    记 fi,0/1f_{i, 0 / 1} 表示当前选出的合法子序列的 MEX 为 ii,且序列中是否有 i+1i+1 的方案数。

    注意到我们每次转移只会用到与 aia_{i} 的差的绝对值不超过 1 的状态,直接转移即可,时间复杂度 O(n)O(n)。

    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    
    constexpr int mod = 1000000007, SZ = 1000001;
    int n, dp0[SZ], dp1[SZ];
    
    signed main() {
      freopen("mexseq.in", "r", stdin);
      freopen("mexseq.out", "w", stdout);
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
      cin >> n;
      for (int i = 0; i < n; ++i) {
        int v;
        cin >> v;
        if (v >= 1) {
          int add =
              (((v >= 1) ? dp0[v - 1] : 0) + ((v >= 1) ? dp1[v - 1] : 0)) % mod;
          dp1[v - 1] = (dp1[v - 1] + (int)add) % mod;
        }
        if (v + 1 < SZ) {
          dp0[v + 1] = (dp0[v + 1] + ((v + 1 < SZ) ? dp0[v + 1] : 0)) % mod;
          dp1[v + 1] = (dp1[v + 1] + ((v + 1 < SZ) ? dp1[v + 1] : 0)) % mod;
        }
        if (v + 1 < SZ)
          dp0[v + 1] = (dp0[v + 1] + ((v < SZ) ? dp0[v] : 0)) % mod;
        if (!v)
          dp0[1] = (dp0[1] + 1) % mod;
        else if (v == 1)
          dp1[0] = (dp1[0] + 1) % mod;
      }
      int ans = 0;
      for (int m = 0; m < SZ; ++m) {
        ans = (ans + dp0[m] + dp1[m]) % mod;
      }
      cout << ans << '\n';
      return 0;
    }
    
    • 1

    Information

    ID
    86
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    7
    Tags
    (None)
    # Submissions
    20
    Accepted
    8
    Uploaded By