CF1417-C

题目: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");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值