P8445 射命丸文的取材之旅 题解
题意:给定 $\{a_n\},\{b_n\}$,求 $\max\limits_{1\le l\le r\le n,c_i\in\{a_i,b_i\}}\{r-l+1-\mathop{\operatorname{mex}}\limits_{i=l}^rc_i\}$。
Subtask 1
特殊条件:$1\le n\le 10$。$O(2^nn^2)$ 暴力枚举 $c_i,l,r$ 取值即可。
Subtask 2
特殊条件:$a_i=b_i$。问题转化为给定 $\{c_n\}$,求 $\max\limits_{1\le l\le r\le n}\{r-l+1-\mathop{\operatorname{mex}}\limits_{i=l}^rc_i\}$。
注意到 $0\le c_i\le n$,考虑枚举 $x=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$,对每个 $x\in[0,n]$ 求出满足条件的最大 $r-l+1$。
$\forall i\in[l,r],c_i\neq x$ 是 $x=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$ 的必要条件,但我们可以升序枚举 $x$,然后把它当成充分条件来用。
证明:如果有 $\forall i\in[l,r],c_i\neq x\bigwedge x\neq\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$,那么一定有 $x>\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$,
而更小的 $x'=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$ 已经枚举过,且 $r-l+1-x'>r-l+1-x$,$x$ 不会对答案造成影响。
问题转化为对每个 $x\in[0,n]$ 求 $\max\limits_{1\le l\le r\le n,\forall i\in[l,r],c_i\neq x}r-l+1$。
考虑正序扫一遍 $\{c_n\}$,记录 $p_k$ 为 $k$ 上次出现的位置,$s_k$ 为当前 $\max\limits_{1\le l\le r\le n,\forall i\in[l,r],c_i\neq k}r-l+1$;
扫到 $c_i$ 时,用 $i-p_{c_i}-1$ 更新 $s_{c_i}$($l=p_{c_i}+1,r=i-1$),再更新 $p_{c_i}\gets i$。
$\forall k\in[0,n],p_k=0$,这样就考虑了 $l=1$ 的情况;扫完后,用 $n-p_{k}$ 更新 $s_k$,这样就考虑了 $r=n$ 的情况。
以上,我们对每个 $x\in[0,n]$ 求出了满足 $x=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$ 的最大 $r-l+1$,$O(n)$ 枚举 $x$ 即可。
#include <cstdio>
#include <algorithm>
using namespace std;
int n, q, c[1000050], p[1000050], s[1000050];
int main()
{
scanf("%d", &n);
for(int i = 1;i <= n;++i) scanf("%d", c + i),
s[c[i]] = max(s[c[i]], i - p[c[i]] - 1), p[c[i]] = i;
for(int k = 0;k <= n;++k) s[k] = max(s[k], n - p[k]);
for(int x = 0;x <= n;++x) q = max(q, s[x] - x);
return printf("%d", q), 0;
}
Subtask 3
仍然考虑枚举 $x=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$,对每个 $x\in[0,n]$ 求出满足条件的最大 $r-l+1$。
$\forall i\in[l,r],a_i\neq x\bigvee b_i\neq x$ 是 $x=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$ 的必要条件,升序枚举 $x$,可以把它当成充分条件来用。证明同上。
考虑正序扫一遍 $\{a_n\},\{b_n\}$,当 $a_i=b_i$ 时更新 $s_{a_i}$ 和 $p_{a_i}$。注意考虑 $l=1$ 和 $r=n$ 的情况。
以上,我们对每个 $x\in[0,n]$ 求出了满足 $x=\mathop{\operatorname{mex}}\limits_{i=l}^rc_i$ 的最大 $r-l+1$,$O(n)$ 枚举 $x$ 即可。
#include <cstdio>
#include <algorithm>
using namespace std;
int n, q, a[1000050], b[1000050], p[1000050], s[1000050];
int main()
{
scanf("%d", &n);
for(int i = 1;i <= n;++i) scanf("%d", a + i);
for(int i = 1;i <= n;++i) scanf("%d", b + i);
for(int i = 1;i <= n;++i) if(a[i] == b[i])
s[a[i]] = max(s[a[i]], i - p[a[i]] - 1), p[a[i]] = i;
for(int k = 0;k <= n;++k) s[k] = max(s[k], n - p[k]);
for(int x = 0;x <= n;++x) q = max(q, s[x] - x);
return printf("%d", q), 0;
}

浙公网安备 33010602011771号