Educational Codeforces Round 118 (Rated for Div. 2) D. MEX Sequences
\(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\)只能取这两种值。
列出方程:
\]
\]
但是这样需要\(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\)的方案。
那么方程如下:
\]
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\)的方案。
那么方程如下:
\]
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\)的方案。
那么方程如下:
\]
#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的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- springboot整合mqtt 消费端
用到的工具: EMQX , mqttx , idea 工具使用都很简单,自己看看就能会. 订阅端config代码: package com.example.demo.config; import lo ...
- python=2.7-not available from current channels
现象 使用miniconda3创建python2的环境 Collecting package metadata (current_repodata.json): done Solving enviro ...
- protoc-gen-doc 自定义模板规则详解
protoc-gen-doc 自定义模板规则详解 配套演示工程 此项目中所用 proto 文件位于 ./proto 目录下,来源于 官方proto示例 此项目中所列所有模板case文件位于 ./tmp ...
- SQL-去除最大值与最小值求均值的问题
背景 今天有同事问我一道关于数据库SQL的面试题,我刚开始随便给了一个思路,后来思索发现这个思路有漏洞,于是总结下来,仅供参考. 问题: 薪水表中是员工薪水的基本信息,包括雇员编号,和薪水,查询除去最 ...
- .Net Web API 005 Controller上传小文件
1.附属文件对象定义 一般情况下,系统里面的文件都会附属一个对象存在,例如用户的头像文件,会附属用户对象存在.邮件中的文件会附属邮件存在.所以在系统里面,我们会创建一个附属文件对象,命名为Attach ...
- 小议ml.NET机器学习与人机责任划分
最近,特斯拉宣布召回110万辆车,名义上是纠正单踏板不良习惯,背后原因可能是这些车辆的电子控制单元存在缺陷,可能导致刹车失灵(潮州等交通事故至今没有定论).这个事件引起了人们对于机器学习技术和人机责任 ...
- Programming abstractions in C阅读笔记:p123-p126
<Programming Abstractions In C>学习第50天,p123-p126,总结如下: 一.技术总结 1.notaion 这也是一个在计算机相关书籍中出现的词,但有时却 ...
- 完美解决Content type ‘multipart/form-data;boundary=----------0467042;charset=UTF-8‘ not supported问题
一.前言 今天在做文件上传功能出现了该问题,该接口如下: @PostMapping("/upload") public Boolean upload(@RequestParam ...
- 如何平息WPS for linux启动后,笔记本风扇的怒吼
create:2022-09-06 20:02:45 WPS启动后,点击菜单栏右上角[未同步]按钮,不登录,直接关掉.几秒后,笔记本风扇嗷嗷叫.桌面conky显示wpscloudsvr进程CPU占用8 ...
- Excel单元格快速交换相邻位置内容
一.相邻两列内容交换(A1与B1交换)1.首先选择A1单元格的边框位置,出现了向上下左右的十字标志 2.此时按住shift键,并且拖向B1单元格的右边,出现"工"汉字标志 3.松开 ...