首页 > 其他分享 >Codeforces Round #673 (Div. 2) C. k-Amazing Numbers

Codeforces Round #673 (Div. 2) C. k-Amazing Numbers

时间:2022-10-27 18:57:53浏览次数:71  
标签:673 number Codeforces Amazing length array include amazing

题面

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;
}

标签:673,number,Codeforces,Amazing,length,array,include,amazing
From: https://www.cnblogs.com/cancers/p/16833318.html

相关文章

  • Codeforces Round #828 (Div. 3) A-F
    比赛链接A题解知识点:贪心,模拟。遇到没用过的数字就给个字母,遇到用过的数字就对照字母是否一致。时间复杂度\(O(n)\)空间复杂度\(O(n)\)代码#include<bits/stdc+......
  • Codeforces Round #632 (Div. 2) / 1333C】- C. Eugene and an array
    题面Eugenelikesworkingwitharrays.Andtodayheneedsyourhelpinsolvingonechallengingtask.Anarrayccisasubarrayofanarraybbifcccanbeobta......
  • Codeforces Round #725 (Div. 3) ABC(双指针)F
    https://codeforces.com/contest/1538AB都没啥好说的,C卡了半天,F挺好写的,找到规律了就直接ac1300的题目卡半天,1500的题目写的顺顺利利,真呆啊我A.StoneGame#include<......
  • Codeforces Round #619 (Motarack's Birthday)
    题面DarkisgoingtoattendMotarack’sbirthday.DarkdecidedthatthegiftheisgoingtogivetoMotarackisanarrayaofnnon-negativeintegers.Darkcr......
  • Codeforces Round #829 (Div. 2)
    Contest链接E题意简述给长为\(n\)序列,随机等概率交换两个不同位置(\(i<j\))的值,要求\(a_i>a_j\)时才能交换。\(n\le200000\)像这个题但是强制要求\(a......
  • Codeforces 1672 E. notepad.exe
    题意这是一道交互题,有n个字符串,每个字符串长度:0-2000,n:0-2000有一个机器对他进行排版,你可以给他一个每行的最大宽度w,那么每行只能放长度为w的字符;每行相邻两个字符......
  • Codeforces Round #690 (Div. 3) F
    F.TheTreasureofTheSegments理解题意就是要让我们找一个线段+他相交的所有线段max我们暴力枚举线段然后用sum-不相交的不相交的就好算了只有两种情况一个线段左......
  • Codeforces Round #830 (Div. 2) A-D
    比赛链接A题解知识点:贪心,数论。先求出序列最大公约数\(d\),如果为\(1\)直接输出\(0\)。否则,尝试用最后一个数操作,\(gcd(d,n)=1\)则可以,花费为\(1\)。否则......
  • Educational Codeforces Round 109 (Rated for Div. 2) D
    D.Armchairs我们发现性质这前面的0显然是给第一个1匹配而不会前面0的给第二个后面的给第一个显然不优有了这个性质我们就可以通过0来做文章要是这个位置是0我们显......
  • Codeforces Round #715 (Div. 2) C
    C.TheSportsFestival观察发现我们显然选择一个数字开始后我们拿周围的数字显然存在最优解(sort过)这样就很金典了n=2000我们显然可以暴力区间dp然后将转移只用从拿......