C. k-Amazing Numbers(思维前缀最小值+枚举相同数距离)

https://codeforces.com/contest/1417/problem/C


大晚上的想岔了..

a1,a2,…,an (1≤ai≤n)这个范围其实就在暗示要枚举出现的数。但是我不知道怎么恍惚看成1e9..

对于每个数,实际上就是考虑相同数之间最少需要多长的k才能都包含到。枚举一个数和序列开头,中间数之间的差,最后一个数和序列结尾。用一个ans[k]记录最少需要长度的k中最小的数字是哪个。ans[k]=min(ans[k],i);

最后注意还要维护一下ans[k]的前缀最小。因为长度为2能覆盖到最小值长度>2的必然能覆盖到。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+100;
typedef long long LL;
const LL inf=1e18;
LL ans[maxn]; 
vector<LL>v[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--)
  {
  	LL n;cin>>n;
  	for(LL i=0;i<=n+10;i++) ans[i]=inf,v[i].clear();
  	for(LL i=1;i<=n;i++){
  		LL x;cin>>x;v[x].push_back(i);	
	}
	for(LL i=1;i<=n;i++)
	{
		if(!v[i].empty())
		{
			LL mx=0;
			for(LL j=1;j<v[i].size();j++)
			{
				mx=max(mx,v[i][j]-v[i][j-1]);
			}
			mx=max(mx,v[i].front());
			mx=max(mx,n-v[i].back()+1);
			ans[mx]=min(ans[mx],i);
		}
	}
	for(LL i=2;i<=n;i++) ans[i]=min(ans[i],ans[i-1]);
	for(LL i=1;i<=n;i++){
		if(ans[i]<inf) cout<<ans[i]<<" ";
		else cout<<"-1"<<" ";
	} 
	cout<<endl;
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108854529