CodeForces - 1417C k-Amazing Numbers(思维+前缀和)

题目链接:https://vjudge.net/contest/404677#problem/C
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.

翻译:
给定一个a数组,求:
输出n个数,每个数对应的k-amazing值(1<=k<=n),k-amazing值是这个数组所有长度为k所覆盖的子序列里最小的那个数值(所有长度为k的子序列都存在的一个最小数字)。

分析:
任选一个数,x会出现在任意长度为k的所有子序列中
x在数组中的下标为 p 1 < p 2 < . . . . . < p n p_1<p_2<.....<p_n p1<p2<.....<pn,那么k至少为 p i + 1 − p i p_{i+1}-p_i pi+1pi,当然 k > = p 1 k>=p_1 k>=p1 k > = n − p m + 1 k>=n-p_m+1 k>=npm+1

代码:

#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
const int N=3*1e5+10;
const int inf=0x3f3f3f3f;
int a[N],n;
vector<int>v[N];
int pos[N];///pos[i]:当间距为i时,所有子序列都出现的最小数字为pos[i]
void init()
{
    for(int i=0; i<=n; i++)
    {
        v[i].clear();
        pos[i]=inf;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            v[a[i]].push_back(i);
        }
        for(int i=1; i<=n; i++)///枚举每一个i,找到一个最小的区间k,i在任意子序列k中都出现过
        {
            int mx=0;
            if(!v[i].empty())
            {
                for(int j=1; j<v[i].size(); j++)
                    mx=max(mx,v[i][j]-v[i][j-1]);
                mx=max(mx,v[i].front());
                mx=max(mx,n-v[i].back()+1);///找到i和i之间相差的最大间距,最大间距都能满足,小间距更能满足
                pos[mx]=min(pos[mx],i);
            }
        }
        for(int i=2; i<=n; i++)///区间长度为i的满足题意,长度大于i的更满足
            pos[i]=min(pos[i],pos[i-1]);
        for(int i=1; i<n; i++)
        {
            if(pos[i]!=inf)
                printf("%d ",pos[i]);
            else
                printf("-1 ");
        }
        if(pos[n]!=inf)
            printf("%d\n",pos[n]);
        else
            printf("-1\n");
    }
    return 0;
}

每一刻都是新的开始

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zaiyang遇见

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

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

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

打赏作者

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

抵扣说明:

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

余额充值