1416A k-Amazing Numbers

本文介绍了一种数组处理问题,涉及寻找数组中特定长度子串内的最小值,即k-AmazingNumbers。文章通过示例解释了题目的要求,并提供了C++代码实现,该代码计算每个k的k-AmazingNumbers,对于不存在公共元素的情况返回-1。算法关键在于计算每个数字的最大间隔,并更新答案数组。
摘要由CSDN通过智能技术生成

A. 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⋅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

题意:输入一串数组a,它由n个1到n的整数组成,k-Amazing Numbers就是这个数组中所有长度为k的连续子串中包含的最小的数字,如果没有都包含的数字,那么k-Amazing Numbers为-1。

分析:其实理解了题目意思之后,题目就变得清晰了很多,如果一个数是k-Amazing Numbers,那么每两个这个数字的最长的间隔需要小于等于k。所以我们需要枚举一个数和序列开头,中间数之间的差,最后一个数和序列结尾的最大值,就是这个数的最长的间隔。用ans[k]代表间隔为k的最小的数字。接下来,还需要更新一次ans数组,因为间隔长度小的数也一定能覆盖间隔长度大的。

详细见代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;

int a[300005];
vector<int>p[300005];
int ans[300005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        for(int i=0;i<=n;i++){
            p[i].clear();
            ans[i]=INF;
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            p[a[i]].push_back(i);//存每个数字出现的每一个位置
        }
        for(int i=1;i<=n;i++){//遍历n个数字
            if(!p[i].empty()){
                int t=0;
                for(int j=1;j<p[i].size();j++)
                    t=max(t,p[i][j]-p[i][j-1]);//计算出最大的间隔
                t=max(max(t,p[i].front()-1+1),n-p[i].back()+1);//该数出现的第一个位置和序列首端的间隔和该数出现的最后一个位置和序列末端的间隔的最大值
                ans[t]=min(ans[t],i);//记录间隔为t的最小数字
            }
        }
        for(int i=2;i<=n;i++)ans[i]=min(ans[i],ans[i-1]);//间隔长度小的数也一定能覆盖间隔长度大的。
        for(int i=1;i<=n;i++){
            if(ans[i]==INF)printf("-1 ");
            else printf("%d ",ans[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a碟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值