【题解】CF1613D MEX Sequences
FifthAxiom · · 题解
题目大意:
对于一个非负整数序列
Solution
首先题意中有一个坑点:
我们定义满足
\forall i=1\cdots n 都有a_i\le|\operatorname{MEX}(a_1,a_2,\cdots,a_i)| 的序列为合法序列。
因此我们必须按原序统计。
考虑设计一个暴力的 dp ,设
但是这个 dp 是
设
===========
1. 前
1.1. 前
1.2. 前
1.3. 前
===========
2. 前
2.1 前
2.2前
===========
3. 前
3.1 前
3.2 前
3.3 前
剩下的所有状态都只有从前面的
最后的答案为
发现同一阶段的状态都可以直接由上一阶段的状态转移过来,因此我们可以舍去第一维,边读入边转移。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
typedef long long LL;
const int N = 5e5 + 10, P = 998244353;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
char getc () {return gc();}
inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
template <class I> inline void gi (I &x) {for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;}
template <class I> inline void print (I x) {if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;while (x) qu[++ qr] = x % 10 + '0', x /= 10;while (qr) putc (qu[qr --]);}
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::gi; using io::putc; using io::print; using io::getc; using io::flush;
int n;
LL f[N][2];
int power(int a, int b) {
int res = 0;
for (; b; b >>= 1) {
if (b & 1) res = (LL)res * a % P;
a = (LL)a * a % P;
}
}
int main() {
//ios::sync_with_stdio(false), cin.tie(NULL);
int T;
gi(T);
while (T--) {
gi(n);
for(int i = 0; i <= n; i++) f[i][0] = f[i][1] = 0;
f[0][0] = 1;//我们这里为了方便计算直接设f(0,0)=1,注意最后算答案时减回来
for (int i = 1; i <= n; i++) {
int x;
gi(x);
//mex = x + 1
f[x + 1][0] = f[x + 1][0] * 2 % P;
f[x + 1][1] = f[x + 1][1] * 2 % P;
//mex = x
f[x + 1][0] = (f[x + 1][0] + f[x][0]) % P;
//mex = x - 1
if (x) {
f[x - 1][1] = f[x - 1][1] * 2 % P;
f[x - 1][1] = (f[x - 1][1] + f[x - 1][0]) % P;
}
}
LL res = -1;
for (int i = 0; i <= n; i++) res = (res + f[i][0] + f[i][1]) % P;
print(res);
putc('\n');
flush();
}
return 0;
}