首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【NBUToj】1667 - Hkhv Loves Sequences(模拟,严格递增子串)

【NBUToj】1667 - Hkhv Loves Sequences(模拟,严格递增子串)

作者头像
FishWang
发布2025-08-27 08:52:18
发布2025-08-27 08:52:18
11700
代码可运行
举报
运行总次数:0
代码可运行
  • [1667] Hkhv Loves Sequences

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • Hkhv has a sequence a, consisting of n integers. We'll call a sequence ai,ai+1,...,aj (1<= i<= j<= n) a subsegment of the sequence a. The value (j-i+1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find.
  • 输入
  • The first line contains integer n (1 <=n <= 10^5). The next line contains n integers a1,a2,...,an (1 <=ai <= 10^9).
  • 输出
  • In a single line print the answer to the problem — the maximum length of the required subsegment.
  • 样例输入
  • 6 7 2 3 1 5 6
  • 样例输出
  • 5
  • 提示
  • You can choose subsegment a2,a3,a4,a5,a6 and change its 3rd element (that is a4) to 4.
  • 来源
  • Recoder

好开心啊,居然AC了。不过题目表述不太清楚吧?改的数可以改成负数吗?(默认为可以就AC了)

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
	int from,to;
	int l;
}data[100011];
int main()
{
	int u;
	int n;
	int num;
	int a[100011];
	int ans;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%d",&n);
		for (int i = 1 ; i <= n ; i++)
			scanf ("%d",&a[i]);
		num = 1;
		ans = 1;
		data[num].from = 1;
		data[num].l = 1;
		for (int i = 2 ; i <= n ; i++)
		{
			if (a[i] > a[i-1])
			{
				data[num].l++;
				data[num].to = i;
				ans = max (ans,data[num].l);
			}
			else
			{
				num++;
				data[num].from = i;
				data[num].to = i;
				data[num].l = 1;
			}
		}
		if (ans == n || ans == n-1)		//特判一下
		{
			printf ("%d\n",n);
			continue;
		}
//		if (ans == n - 1)		//题目不清晰,不知道是否可以把该数改为负数 
//		{
//			if ((a[1] < a[2]) || (a[1] >= a[2] && a[2] != 0))
//				printf ("%d\n",n);
//			else
//				printf ("%d\n",ans);
//			continue;
//		}
		ans++;
		for (int i = 1 ; i < num ; i++)
		{
			if (data[i+1].l == 1 || data[i].l == 1)		//子串长度为1挺麻烦的,单独处理下 
			{
				ans = max (ans , data[i+1].l + 1);
				ans = max (ans , data[i].l + 1);
				if (data[i+1].l == 1 && i != num-1)
				{
					if (a[data[i+1].to-1] + 1 < a[data[i+2].from])
						ans = max (ans , data[i].l + data[i+2].l + 1);
				}
			}
			else
			{
				if (a[data[i].to-1] + 1 < a[data[i+1].from])		//修改前一个子串的最后一个数 
					ans = max (ans , data[i].l + data[i+1].l);
				if (a[data[i+1].from-1] + 1 < a[data[i+1].from+1])		//修改后一个子串的第一个数 
					ans = max (ans , data[i].l + data[i+1].l);
			}
		}
		printf ("%d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档