Codeforces Round #673 (Div. 1)A. k-Amazing Numbers

参考大佬的博客https://blog.csdn.net/weixin_44582673/article/details/108964461?biz_id=102&utm_term=Codeforces%20Round%20#673%20Div.%201A&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-108964461&spm=1018.2118.3001.4187

题目

传送门
A. k-Amazing Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

输入
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
输出
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1

题意:提议比较难懂,读题读了好大一会…给你一个长度为n的序列,对于k∈【1,n】问在每个连续的含有k个元素的子序列中都出现的最小数字是多少,这个数字可能不存在.

思路:我们可以对于一个数字求他的最大间隔t,对于所有的k>=t,这个数字都适用,我们只要找出适用于k的最小数字即可,具体看代码.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f;
int a[350000],b[350000],c[350000],d[350000];
int solve()
{
    int n;
    cin>>n;
    memset(b,0,sizeof(b));//b中存的是序列中每个数上次在序列中出现的位置,初始化为零可以将数字第一次出现的位置视为间隔
    memset(c,0,sizeof(c));//c中存每个数的最大间隔
    memset(d,inf,sizeof(d));//d中存对于每个间隔对应的最小数字
    for(int i=1;i<=n;i++)
     cin>>a[i];
    for(int j=1;j<=n;j++)
    {
        c[a[j]]=max(c[a[j]],j-b[a[j]]);
            b[a[j]]=j;//数字的位置
    }
    for(int i=1;i<=n;i++)
        c[i]=max(c[i],n+1-b[i]);//序列从后往前看每个数的第一个间隔
    for(int i=1;i<=n;i++)
         if(i<d[c[i]])d[c[i]]=i;//由于c【i】表示数字i的最大间隔,d[c[i]],表示这个间隔对应的数字i,要选择每个间隔对应的最小数字,
         for(int i=2;i<=n;i++)
           if(d[i]>d[i-1])d[i]=d[i-1];//对于间隔t,t<=k的数字都适用于k,对与每个k我们要选择最小的数字
         for(int i=1;i<=n;i++)
             {if(d[i]==inf)printf("-1");
               else printf("%d",d[i]);
               if(i!=n)printf(" ");}
               printf("\n");
}
int main()
{
    ios::sync_with_stdio(0);
   int t;
   cin>>t;
   while(t--)
   {
       solve();
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值