\(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. ubuntu 20.04 网络配置

    参考链接:ubuntu 20.04 网络配置 网络配置文件目录:/etc/netplan/ 配置实例 # 静态 network: version: 2 ethernets: ens33: addres ...

  2. 深入Scikit-learn:掌握Python最强大的机器学习库

    本篇博客详细介绍了Python机器学习库Scikit-learn的使用方法和主要特性.内容涵盖了如何安装和配置Scikit-learn,Scikit-learn的主要特性,如何进行数据预处理,如何使用 ...

  3. 趣图|代码重构前vs重构后

    前言 很多程序员对自己写的代码平时很随心所欲,但当有一天让他维护他人的代码,他就会抓狂,很容易激发他体内重构的瘾.(大多数程序员审阅完别人代码后,先会忍不住吐槽一番,然后会忍不住想重构一把,) 在我看 ...

  4. java使用apache.poi导出word文件

    功能说明: 将试卷导出word,并可以打印,装订,效果图: 下面是实现代码: package com.xxxxx.business.course.utils; import com.alibaba.f ...

  5. 【游戏开发笔记】编程篇_C#面向对象{上}

    @ 目录 1.变量和表达式 1.1注释 1.2C#控制台程序基本结构 1.3变量(从存储长度来看) 1.4变量的命名 1.5字面值 1.6运算符 2流程控制 2.1分支 2.2循环 3变量知识拓展 3 ...

  6. 论文解读(Moka‑ADA)《Moka‑ADA: adversarial domain adaptation with model‑oriented knowledge adaptation for cross‑domain sentiment analysis》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:Moka‑ADA: adversarial domain adaptation  with model‑o ...

  7. CVE-2023-2825-GitLab目录穿越poc

    Gitlab CVE-2023-2825 目录穿越漏洞 前言 昨天 GitLab 出了一个版本目录穿越漏洞(CVE-2023-2825),可以任意读取文件.当时我进行了黑盒测试并复现了该漏洞. &qu ...

  8. [apue] 进程环境那些事儿

    main 函数与进程终止 众所周知,main 函数为 unix like 系统上可执行文件的"入口",然而这个入口并不是指链接器设置的程序起始地址,后者通常是一个启动例程,它从内核 ...

  9. springboot3框架搭建

    Spring Boot 3.0.0已经发布一段时间了,越来越多的公司考虑将技术框架升级到最新版本,JDK也相应要求JDK17以上.对应Spring Boot 2.x的版本,建议先升级到Spring B ...

  10. 再聊Java Stream的一些实战技能与注意点

    大家好,又见面了. 在此前我的文章中,曾分2篇详细探讨了下JAVA中Stream流的相关操作,2篇文章收获了累计 10w+阅读.2k+点赞以及 5k+收藏的记录.能够得到众多小伙伴的认可,是技术分享过 ...