\(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. 从一道面试题来谈谈Golang中的 ==

    写这篇文章的时候,已经离我找工作有一段时间了,但是觉得这道题不管是面试还是日常的工作中,都会经常遇到,所以还是特意写一篇文章,记录下自己对Golang中==的理解.如文章中出现不对的地方,请不吝赐教, ...

  2. Linux下apt与dpkg的详解

    apt是一个包管理工具,用于管理Debian和Ubuntu等基于Debian的Linux发行版中的软件包.它是"Advanced Packaging Tool"的缩写,允许用户在系 ...

  3. [jmeter]快速入门

    前言 以压测一个api为例.假设有一个非常简单的api http://192.168.1.111:8080/,只是返回 helloworld 字符串. 准备 打开 jmeter,新建测试计划 在测试计 ...

  4. 从浅入深了解.NET Core MVC 2.x全面教程【第二章】

    二.Logging 1.诊断中间件 命名空间:Microsoft.AspNetCore.Diagnostics 报告信息并处理异常 2.诊断中间件 UseDeveloperExceptionPage: ...

  5. 论文解读(SimGCL)《Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for Recommendation》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:Are Graph Augmentations Necessary? Simple Graph Contr ...

  6. java学习阶段一

    扩展名默认没有打开 FIRST APP public class HelloWorld { public static void main (String[] args){ System.out.pr ...

  7. Android13深入了解 Android 小窗口模式和窗口类型

    Android13深入了解 Android 小窗口模式和窗口类型 小窗模式,作为一种在移动设备上的多任务处理方式,为用户带来了便捷和高效的体验,尤其在一些特定场景下,其价值愈发凸显.以下是为什么需要小 ...

  8. 干掉 CRUD!这个API开发神器效率爆炸,无需定义MVC类!!

    简介 magic-api 能够只通过 UI 界面就能完成简单常用的接口开发,能够支持市面上多数的关系性数据库,甚至还支持非关系性数据库 MongoDB. 通过 magic-api 提供的 UI 界面完 ...

  9. 如何使用关键词搜索API接口获取到快手的商品

    如果您想使用关键词搜索API接口获取到快手的商品,可以通过以下步骤实现: 1. 首先注册账号.根据文档申请相应的接口权限. 2. 确定需要使用的API接口.对于商品搜索,您可以查看相关的API文档以获 ...

  10. Codeforces 1462F The Treasure of The Segments

    题意 给\(n(1\leq n\leq 2*10^5)\)个线段$[l_i,r_i] (1≤l_i≤r_i≤10^9) $,问最少删除几个线段,使得剩下线段中,有至少一个线段与所有线段相交. 分析 对 ...