题解:P12538 [XJTUPC 2025] 泰拉构史
chenhanzheapple · · 题解
传送门
思路
首先注意到一个数最多被交换
所以考虑 DP。
设
因为一个位置合法的状态并不多(因为
转移方程是简单的,只需要考虑可以和要不要交换即可,详情请见代码。
代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n;
const int MOD = 998244353;
int a[1000005];
map<pair<int,int>,int> f[1000005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i];
}
f[0][{-1e18,-1e18}] = 1;
for(int i=0;i<n;i++){
for(auto x:f[i]){
auto y = x.first;
f[i+1][{y.second,a[i+1]}] = (f[i+1][{y.second,a[i+1]}]+x.second)%MOD;
if(abs(a[i+1]-y.second)==1){
f[i+1][{a[i+1],y.second}] = (f[i+1][{a[i+1],y.second}]+x.second)%MOD;
if(abs(a[i+1]-y.first)==1){
f[i+1][y] = (f[i+1][y]+x.second)%MOD;
}
}
}
}
int ans = 0;
for(auto x:f[n]){
ans = (ans+x.second)%MOD;
}
cout << ans;
return 0;
}