题目:C - k-Amazing Numbers
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⋅10^5) — 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⋅10^5.
Output:
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example:
思路:
题目问我们,长度为k的所有子串中公有的最小数字,没有公有输出-1,暴力应该会超时,做个预处理,预处理每一个数字,找到使他能被包含在所有k长子串的,最小的k,然后开个优先队列que[k].将其存入,当i-th大于k时这个数字也是可行的。然后稍加判断输出答案。
代码:
#include<bits/stdc++.h>
using namespace std;
int us[300005];
int a[300004];
int l[300004]={0};
int p[300004]={0};
int u[300004];
int uu[300004];
priority_queue <int,vector<int>,greater<int> > q[300004];
int main(){
int t;
cin>>t;
for(int i=1;i<=t;i++){
int n;
cin>>n;
int s=0;
for(int j=1;j<=n;j++){
while(!q[j].empty())
q[j].pop();
l[j]=0;
us[j]=1e9+7;
p[j]=0;
scanf("%d",&a[j]);
if(u[a[j]]==0){
uu[s]=a[j];
u[a[j]]=1;
s++;
}
}
for(int j=1;j<=n;j++){
if(j-l[a[j]]>p[a[j]])
p[a[j]]=j-l[a[j]];
l[a[j]]=j;
}
for(int j=0;j<s;j++){
if(n-l[uu[j]]+1>p[uu[j]])
p[uu[j]]=n-l[uu[j]]+1;
q[p[uu[j]]].push(uu[j]);
}
int o=-1;
for(int j=1;j<=n;j++){
u[a[j]]=0;
if(q[j].empty()&&o==-1)
printf("-1 ");
else if(q[j].empty()&&o>0)
printf("%d ",o);
else if(!q[j].empty()&&o==-1){
o=q[j].top();
q[j].pop();
printf("%d ",o);
}
else if(!q[j].empty()&&o>0){
if(q[j].top()<o)
o=q[j].top();
q[j].pop();
printf("%d ",o);
}
}
printf("\n");
}
}