Giant Sequoia(UTPC Contest 02-25-22 Div. 2 (Beginner))


UTPC Contest 02-25-22 Div. 2 (Beginner) F: 题目链接.

题目

Alfred, an avid botanist and lumberjack, while traveling across the state of California, has come across an absolutely humongous giant sequoia tree. Being the botanist that he is, Alfred would like to examine this fascinating giant sequoia tree in great depth. More specifically, he would like to observe its branches.
Each of the n branches of the giant sequoia tree has a specific length associated with it, and certain branches are longer than others. Because of this, Alfred was inspired to solve a certain problem, though his skills in computing are quite limited, and he has come to you for help!
Alfred would like to figure out the longest strictly-increasing consecutive sub-array (a sub-array such that ai+1>ai for each i except the last in the sub-array) of the giant sequoia tree branches. However, he is torn between his love for botany and his duties as a lumberjack. He can either choose to leave the tree alone and compute the answer directly, or he can chop off a consecutive sub-array of size exactly k from the giant sequoia tree before running his computations. More formally, Alfred can choose some index i such that i+k−1≤n and then remove the branches at indices i,i+1,i+2,…,i+k−1. Since he doesn’t have all that much energy after hiking all day, he can only perform this action a maximum of one time.
Given his choices, can you help Alfred find the longest strictly-increasing consecutive sub-array within the giant sequoia tree after at most one chop of size k ?

翻译(机翻)

Alfred,一个狂热的植物学家和伐木工人,在穿越加利福尼亚的时候,遇到了一棵绝对巨大的巨型红杉树。作为一个植物学家,阿尔弗雷德想深入研究这棵迷人的巨杉树。更具体地说,他想观察它的树枝。
巨型红豆杉树的每一个分支都有一个特定的长度,某些分支比其他分支长。正因为如此,阿尔弗雷德受到启发,想解决某个问题,尽管他在计算方面的技能相当有限,他已经向你寻求帮助了
Alfred想算出巨型红杉树树枝中最长的严格递增的子数(一个子数,使ai+1>ai的每个i,除了子数的最后一个)。然而,他在对植物学的热爱和作为一个伐木工人的职责之间纠结。他可以选择不理会这棵树,直接计算出答案,或者他可以在运行计算之前,从巨杉树上砍下一个大小正好为k的连续子数组。更正式地说,Alfred可以选择某个索引i,使i+k-1≤n,然后删除索引i,i+1,i+2,…,i+k-1处的分支。由于他爬了一天的山,没有那么多的精力,他最多只能做一次这个动作。
鉴于他的选择,你能帮助Alfred在最多一次k大小的砍伐后,找到巨杉树内最长的严格递增的连续序列吗?

思路

用一个数组来标记一下上升序列的起点,如 序列 [1,2,3,2,5] ,其对应的起点数组为: [0,0,0,3,3],在预处理时,先统计不砍的长度,后面在分成两段统计上升长度

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int dp[maxn], a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
    dp[1] = 1;
    int ans = 1;
    // 预处理起点,以及不砍的最大长度
    for (int i = 2; i <= n; ++i)
    {
        if (a[i] >= a[i - 1])
        {
            dp[i] = dp[i - 1];
        }
        else
        {
            dp[i] = i;
        }
        if (i - dp[i] + 1 > ans)
        {
            ans = i - dp[i] + 1;
        }
    }
    // 循环砍的结果
    for (int i = 2; i <= n - k + 1; i++)
    {
        // 砍部分的左边最大小于砍部分的右边最小,构成上升
        if (a[i + k] > a[i - 1])
        {
            int right = i + k;
            int left = i - 1;
            int count = 0;
            // 统计长度
            for (int i = right; i <= n; i++)
            {
                if (dp[i] != dp[right])
                {
                    break;
                }
                count++;
            }
            for (int i = left; i >= 1; i--)
            {

                if (dp[i] != dp[left])
                {
                    break;
                }
                count++;
            }
            ans = max(ans, count);
        }
    }
    cout << ans << endl;
    // return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值