题目链接:https://vjudge.net/contest/404677#problem/C
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.
翻译:
给定一个a数组,求:
输出n个数,每个数对应的k-amazing值(1<=k<=n),k-amazing值是这个数组所有长度为k所覆盖的子序列里最小的那个数值(所有长度为k的子序列都存在的一个最小数字)。
分析:
任选一个数,x会出现在任意长度为k的所有子序列中
x在数组中的下标为
p
1
<
p
2
<
.
.
.
.
.
<
p
n
p_1<p_2<.....<p_n
p1<p2<.....<pn,那么k至少为
p
i
+
1
−
p
i
p_{i+1}-p_i
pi+1−pi,当然
k
>
=
p
1
k>=p_1
k>=p1和
k
>
=
n
−
p
m
+
1
k>=n-p_m+1
k>=n−pm+1
代码:
#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
const int N=3*1e5+10;
const int inf=0x3f3f3f3f;
int a[N],n;
vector<int>v[N];
int pos[N];///pos[i]:当间距为i时,所有子序列都出现的最小数字为pos[i]
void init()
{
for(int i=0; i<=n; i++)
{
v[i].clear();
pos[i]=inf;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
v[a[i]].push_back(i);
}
for(int i=1; i<=n; i++)///枚举每一个i,找到一个最小的区间k,i在任意子序列k中都出现过
{
int mx=0;
if(!v[i].empty())
{
for(int 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);///找到i和i之间相差的最大间距,最大间距都能满足,小间距更能满足
pos[mx]=min(pos[mx],i);
}
}
for(int i=2; i<=n; i++)///区间长度为i的满足题意,长度大于i的更满足
pos[i]=min(pos[i],pos[i-1]);
for(int i=1; i<n; i++)
{
if(pos[i]!=inf)
printf("%d ",pos[i]);
else
printf("-1 ");
}
if(pos[n]!=inf)
printf("%d\n",pos[n]);
else
printf("-1\n");
}
return 0;
}
每一刻都是新的开始