Codeforces Round #673 (Div. 2) 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.

题意

对于每一个k∈[1,n],找出数列在任意连续k个数中都出现的数的最小值

分析

若某个数满足在连续k个数中出现,则任意两个这个数之间的距离小于等于k;
(当然也要考虑到这个数与起始和结尾的位置)
那么可以先处理每个数的相邻两数的最大距离(要特殊处理一下第一个和最后一个与边界的距离)
最后贪心的从小到大枚举即可

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
inline int read() {
	register int x = 0, f = 1;
	register char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0',c = getchar();
	return x * f;
}
const int MAXN = 3e5 + 1;
int n;
int a[MAXN], last[MAXN], dis[MAXN], ans[MAXN];
int main() {
	int t = read();
	while (t--){
		memset(last, 0, sizeof(last));
		memset(dis, 0, sizeof(dis));
		memset(ans, -1, sizeof(ans));
		n = read();
		for (int i(1); i <= n; i++) a[i] = read();
		for (int i(1); i <= n; i++){
			dis[a[i]] = max(dis[a[i]], i - last[a[i]]);
			last[a[i]] = i;
		}
		for (int i(1); i <= n; i++)
			dis[i] = max(dis[i], n + 1 - last[i]);
		for (int i(1); i <= n; i++)
			for (int j(dis[i]); j <= n && ans[j] == -1; j++)//举个例子 对于1如果k = 3可以,那么k更大时也可以
				ans[j] = i;
		for (int i(1); i <= n; i++)
			printf("%d ",ans[i]);
		puts("");
	}
	return 0;
}

posted @ 2022-10-27 18:45  ancer  阅读(29)  评论(0)    收藏  举报