[容斥][单调栈][dp][神仙题]CF1591F
Little_corn · · 个人记录
题面:this
solution:
容斥神仙题qwq
考虑全集-补集,此时补集就是一些集合的并,可使用容斥
设至少
直接求不好求,考虑转化:
有
此时自然而然的可以定义出:
所以此时可以设计出一个状态转移方程:
时间复杂度为
此时可以发现:
所以可以把第二维压成
接着观察一下式子,发现
接下来关键的一步,就是回到
因此,考虑拆
可以发现一个性质:
对于一个下标
此时维护一个单调栈即可在
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10,mod=998244353;
int n,a[N];
int stk[N],top;
int f[N][2],s[N][2];
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
f[0][0]=s[0][0]=1,f[0][1]=s[0][1]=0;
for(int i=1;i<=n;i++){
while(a[stk[top]]>=a[i]&&top)top--;
stk[++top]=i;
for(int j=0;j<2;j++){
f[i][j]=((top==1?0:f[stk[top-1]][j])+(s[i-1][j^1]-(top==1?0:s[stk[top-1]-1][j^1])+mod)*a[i])%mod;
s[i][j]=(s[i-1][j]+f[i][j])%mod;
}
}
cout<<(f[n][0]-f[n][1]+mod)*((n&1)?(-1+mod):1)%mod;
return 0;
}