\(DP\)真的太难了啊!!

首先考虑到\(f(i, s)\)表示,从前\(i\)个数中选,最后一个数为\(a_i\),且\(MEX(a_1,....,a_i) = \left\{ \begin{aligned} a_{i} - 1 (s = 0) \\ a_{i} + 1(s = 1)\end{aligned} \right.\),因为有\(a_i\)的存在,那么\(MEX\)只能取这两种值。

列出方程:

\[f(i, a[i] - 1) = \sum\limits_{j = 1}^{i - 1}f(j, a[j] - 1)[a_j == a_i] + \sum\limits_{j = 1}^{i - 1}f(j, a[j] + 1)(a_j == a_i - 2)
\]
\[f(i, a[i] + 1) = \sum\limits_{j = 1}^{i - 1}f(j, a[j] - 1)[a_j == a_i + 2] + \sum\limits_{j = 1}^{i - 1}f(j, a_j + 1)(a_j == a_i)
\]

但是这样需要\(O(n ^ 2)\)复杂度。

而发现给定的\(a_i\)值很小,因此可以直接把这个作为状态。

\(f(j, s)\)表示从前i个数中选,\(MEX(...a_k) = j\),且最后一个数为\(a_k\),\(a_k = \left\{ \begin{aligned} j - 1 (s = 0) \\ j + 1(s = 1)\end{aligned} \right.\)的方案数,那么当前x影响的只有\(f(x + 1, s)\)与\(f(x - 1, s)\)这两种方案,这样复杂度就降为了\(O(n * 2)\)

下面进行分类讨论:

1. 若\(MEX = x + 1\),最后一个数为\(x\)的方案。

1.1 前\(i - 1\)个数\(MEX = x + 1\),最后一个数为\(x\)的方案。

1.2 前\(i - 1\)个数\(MEX = x + 1\),最后一个数为\(x\),再添加一个\(x\)的方案。

1.3 前\(i - 1\)个数\(MEX = x\),最后一个数为\(x - 1\),再添加一个\(x\)的方案。

那么方程如下:

\[f(x + 1, 0) = 2 * f(x + 1, 0) + f(x, 0)
\]

2. 若\(MEX = x + 1\),最后一个数为\(x + 2\)的方案。

2.1 前\(i - 1\)个数\(MEX = x + 1\),最后一个数为\(x + 2\)的方案。

2.2 前\(i - 1\)个数\(MEX = x + 1\),最后一个数为\(x + 2\),再添加一个\(x\)的方案。

那么方程如下:

\[f(x + 1, 1) = 2 * f(x + 1, 1)
\]

3. 若\(MEX = x - 1\),最后一个数为\(x\)的方案。

3.1 前\(i - 1\)个数\(MEX = x - 1\),最后一个数为\(x\)的方案。

3.2 前\(i - 1\)个数\(MEX = x - 1\),最后一个数为\(x\),再添加一个\(x\)的方案。

3.3 前\(i - 1\)个数\(MEX = x - 1\),最后一个数为\(x - 2\),再添加一个\(x\)的方案。

那么方程如下:

\[f(x - 1, 1) = 2 * f(x - 1, 1) + f(x - 1, 0)
\]
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int Mod = 998244353; int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
//memset(f, 0, sizeof f);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<ll>> f(n + 2, vector<ll>(4, 0));
f[0][0] = 1;
//f[0][1] = ;
for (int i = 0; i < n; i++) {
int x = a[i];
f[x + 1][0] = (f[x + 1][0] * 2 % Mod + f[x][0]) % Mod;
f[x + 1][1] = f[x + 1][1] * 2 % Mod; if (x > 0) {
f[x - 1][1] = (f[x - 1][1] * 2 % Mod + f[x - 1][0]) % Mod;
}
} ll res = 0;
for (int i = 0; i <= n; i++) {
res = (res + f[i][0] + f[i][1]) % Mod;
} cout << (res - 1 + Mod) % Mod << "\n";
} return 0;
}

Educational Codeforces Round 118 (Rated for Div. 2) D. MEX Sequences的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. React: husky > pre-push hook failed (add --no-verify to bypass)

    解决方案 提交commit和推送代码时都加上--no-verify参数,然他跳过检查 提交 推送

  2. .NET周刊【7月第5期 2023-07-30】

    国内文章 PaddleSharp:跨越一年的版本更新与亮点 https://www.cnblogs.com/sdflysha/p/20230724-paddlesharp-in-a-year.html ...

  3. html标签tr td是什么意思

    <table>代表表格</table><tr>代表表格中的一行</tr><td>代表表格中的一列</td>'tr'与'td'交成 ...

  4. 洛谷 P1122 最大子树和 题解

    一道入门的树形DP. 首先我们对于数据进行有序化处理,这便于我们利用数据结构特点(可排序性)来发觉数据性质(有序.单调.子问题等等性质),以便于后续的转化.推理和处理.有序化可以"转化和创造 ...

  5. IDA的使用2

    IDA的使用2 string类型的选择 Rename 要注意如果再namelist和public name里面是不能重名 操作数 这个主要和开发结合精密, change sign-改变符号 bitwi ...

  6. Inno SetUp安装包:如何在程序安装时卸载驱动程序

    pnputil命令行方式卸载 如果您想通过命令行卸载.INF文件的驱动程序,您需要使用PnPUtil命令.以下是一个示例: pnputil /delete-driver oem0.inf /unins ...

  7. 【Java监控】使用SkyWalking监控Java服务

    你的Java服务是如何监控的呢? 1.Null:监控?什么监控?我一个写代码的服务挂了跟我有什么关系? 2.命令行:服务挂了?内存泄漏?jstat jmap jcmd,还好不是我写的 3.撸代码:Ja ...

  8. 在 RedHat Enterprise、CentOS 或 Fedora Linux 上安装 MongoDB

    在 RedHat Enterprise.CentOS 或 Fedora Linux 上安装 MongoDB 1.大纲 备注:采用yum安装后,所有进程将自动在/usr/bin下,如下的mongo.mo ...

  9. JSTL fn函数使用总结

    首先,我们要在页面的最上方引用: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/function ...

  10. Codeforces Round div.2 C

    Smiling & Weeping ----我对姑娘的喜欢,何止钟意二字 题目链接:Problem - C - Codeforces 自我分析:我感觉这是一道很有意义的题目,可以帮我们更好的理 ...