好题记录
AT_arc115_e [ARC115E] LEQ and NEQ
神仙容斥 DP 题
防止有人标 c,讲讲 \(O(nlogV)\) 做法,你把考虑暴力 dp
写成
线段树维护区间乘 \(-1,0\) 和区间加即可。(线段树 \(2\) 拉过来就行了)
值域在这里一点也不好做,考虑将它消掉。
考虑正难则反,维护不满足的个数,即 \(b_i=b_{i+1}\) 的个数,这里为了不重不漏算贡献,容易想到容斥。
注意这个 \(b_i=b_{i+1}\) 的个数给了你什么条件,你将你钦定的哪些点合并起来,是不是剩下了 \(n-j\) 个点,就是维护 \(n-j\) 个相同颜色的段(注意,这里并不需要保证相邻段颜色不同,因为容斥是至少,就是需要包含)
我们可以设出与值域无关的 dp 状态了,我们让 \(dp_{i,j}\) 为到第 \(i\) 个数,分了 \(j\) 个段。
转移式为:
时间复杂度精细实现为 \(O(n^2)\)
目前的状态还是不行,我们继续考虑优化。
注意到容斥的系数是奇数为负,偶数为正的,我们可以考虑将其奇偶分类,直接将第二维变成 \(0/1\)。
为什么是对的呢,所有转移都与当层无关不会出现环,转移超出范围怎么办,发现是不会超的,因为容斥需要的范围与转移次数的范围一模一样都是 \(n\)。
现在状态可以了,考虑优化转移,发现转移也非常困难,你秉持着 mzoj 不满的暴力能过的习惯,发现对它进行单调栈是可以优化常数的,每次获得一段区间的系数,需预处理前缀和。
琢磨这转移,如果单调栈里有数,在他之前的 dp 转移系数不受当前 \(a_i\) 的转移,与之前一样,我们想着直接将之前这一坨记录下来,将式子写出来,发现就是 \(dp_{pos,j}\)
最终式子:
其中 \(sum\) 是 \(dp\) 的前缀和。
通过容斥减掉,答案就是 \(dp_{n,0}-dp_{n,1}\)。
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
#define int long long
using namespace std;
const int N=2e6+10,inf=1e9,M=2e5+9,mod=998244353;
int n,a[N],zhan[N],top,dp[N][2],sum[N][2];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
dp[0][0]=1;
sum[0][0]=1;
for(int i=1;i<=n;i++)
{
while(top && a[zhan[top]]>=a[i])
top--;
for(int j=0;j<=1;j++)
{
if(top==0)
{
dp[i][j]=(sum[i-1][j^1])*a[i]%mod;
}
else
dp[i][j]=((sum[i-1][j^1]-sum[zhan[top]-1][j^1]+mod)%mod*a[i]%mod+dp[zhan[top]][j])%mod;
sum[i][j]=(sum[i-1][j]+dp[i][j])%mod;
}
zhan[++top]=i;
}
int ans=(dp[n][0]-dp[n][1]+mod)%mod;
if(n&1)
ans=ans*(mod-1)%mod;
cout<<ans;
return 0;
}

浙公网安备 33010602011771号