k-Amazing Numbers

126 篇文章 0 订阅

题目:
You are given an array a consisting of n integers numbered from 1 to n.

Let’s define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is −1.

For each k from 1 to n calculate the k-amazing number of the array a.

Input
The first line contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤3⋅105) — the number of elements in the array. The second line contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.

Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.

Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
题解:

#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[300005],d[300005],ls[300005],ans[300005];
int main()
{
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			ls[i]=d[i]=0,ans[i]=-1;
		}
		for(int i=1;i<=n;i++)
		{
			int x=a[i];
			d[x]=max(d[x],i-ls[x]);
			ls[x]=i;
		}
		for(int i=1;i<=n;i++)
		{
			d[i]=max(d[i],n-ls[i]+1);
			for(int j=d[i];j<=n&&ans[j]==-1;j++) ans[j]=i;
		}
		for(int i=1;i<=n;i++) printf("%d ",ans[i]);
		printf("\n"); 
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值