我是靠谱客的博主 悦耳狗,这篇文章主要介绍Educational Codeforces Round 118 (Rated for Div. 2) D. MEX Sequences(计数dp,现在分享给大家,希望可以做个参考。
已知插入的x会存在两种情况
- 0 0 1 1 2 2
- 0 0 1 3 3
考虑dp,dp[n][2],dp[x][0]表示第一种情况,dp[x][1]表示第二种情况
dp[x]表示当前插入的为x并且以x结尾
首先考虑第一种情况:
1.考虑与dp[x-1][0]组合
2.考虑与dp[x][0]组合(pre)
考虑第二种情况
1.考虑与dp[x-1][1]组合
2.考虑与dp[x][1]组合(pre)
额外考虑的是 须计算对dp[x+2][1]的贡献 即*2
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> using namespace std; #define int long long const int mod=998244353; const int N=5e5+10; int t,n,a[N]; int dp[N][2]; void solve(){ scanf("%lld",&n); dp[0][0]=1; for(int i=1;i<=n;i++){ int x;scanf("%lld",&x);x++; dp[x][0]=(dp[x][0]*(int)2)%mod; dp[x][0]=(dp[x][0]+dp[x-1][0])%mod; // dp[x][0]=(dp[x][0]+dp[x-1][0])%mod; if(x>=2) dp[x][1]=(dp[x][1]*(int)2%mod+dp[x-2][0])%mod; if(x<=n-2) dp[x+2][1]=(dp[x+2][1]*2)%mod; } // cout<<dp[1][0]<<endl; long long sum=0; for(int i=1;i<=n+1;i++){ sum+=(dp[i][0]+dp[i][1])%mod; sum%=mod; dp[i][0]=dp[i][1]=0; } cout<<sum<<endl; } int32_t main(){ scanf("%lld",&t); while(t--){ solve(); } }
最后
以上就是悦耳狗最近收集整理的关于Educational Codeforces Round 118 (Rated for Div. 2) D. MEX Sequences(计数dp的全部内容,更多相关Educational内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复