跳到正文

题解

题解:AT_agc017_c [AGC017C] Snuke and Spells

sLMxf约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年8月22日
收录
2026年8月22日
更新
2026年11月8日
篇幅
约 2 分钟

版本

  1. v1当前

首先发现了一个有用的性质:如果数组是 1∼n1\sim n 的排列,则答案为 00。

问题是如何解决出现了重复数字,如果有 cntcnt 个 xx,我们考虑将一次全删改为多次单删。具体的,考虑重新映射,把 cntcnt 个 xx 改为 x,x−1,⋯ ,x−cnt+1x,x-1,\cdots,x-cnt+1。此时重复的牌就是不合法的,我们只需要统计序列中没有出现的牌数量即可。

具体的,每种牌 xx 视作覆盖了区间 [x−cntx+1,x][x-cnt_x+1,x],答案就是没有覆盖的点的位置。

可以线段树,也可以直接写,时间复杂度 O(n+m)O(n+m)。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,a[N],t[N],b[N];
signed main()
{
	int m,x,y,ans=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		t[a[i]]++;
	}
	for(int i=1;i<=n;i++)
		for(int j=max(i-t[i]+1,1);j<=i;j++)
		{
			ans+=(b[j]==0);
			b[j]++;
		}
	while(m--)
	{
		cin>>x>>y;
		if(a[x]-t[a[x]]+1>0) b[a[x]-t[a[x]]+1]--,ans-=(b[a[x]-t[a[x]]+1]==0);
		t[a[x]]--;
		a[x]=y;
		t[a[x]]++;
		if(a[x]-t[a[x]]+1>0) ans+=(b[a[x]-t[a[x]]+1]==0),b[a[x]-t[a[x]]+1]++;
		cout<<n-ans<<'\n';
	}
	return 0;
}
原文
存档评论1 条抓取于