跳到正文

题解

题解:P11837 [USACO25FEB] Making Mexes B

cff_0102约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年3月5日
收录
2025年12月12日
更新
2026年11月5日
篇幅
约 1 分钟
获赞
2
题目
P11837

版本

  1. v1当前

[2025-03-06 15:04:38] 管理员【MatrixGroup】审核拒绝了该文章, 原因: 为什么显然了|审核管理员:MatrixGroup,对审核结果有疑问请私信交流


搞一个桶 ss,sis_i 记录 aa 里面有几个 ii。

要让 aa 的 mex⁡\operatorname{mex} 为 xx,需要使得修改后的 ss 满足:

  • ∀1≤i<x,si≠0\forall1\le i<x,s_i\ne0;
  • sx=0s_x=0。

因此,设 bxb_x 表示 ∑i=0x−1[si=0]\sum\limits_{i=0}^{x-1}[s_i=0],那么显然让 aa 的 mex⁡\operatorname{mex} 为 xx 所需要的最少操作次数就是 max⁡(bx,sx)\max(b_x,s_x)。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N],b[N],s[N];
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i],s[a[i]]++;
	for(int i=1;i<=n;i++)b[i]=b[i-1]+(s[i-1]==0);
	for(int i=0;i<=n;i++){
		cout<<max(b[i],s[i])<<"\n";
	}
	return 0;
}
原文