跳到正文

题解

increase

lighthing约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年10月13日
收录
2026年8月27日
更新
2026年11月17日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

题目

先从1开始找,只要合适,就加入,一直找到尾,再保存最大值。

C++
#include<bits/stdc++.h>
using namespace std;
int n,a[100001],ans;
void dfs(int x,int step,int cnt){
	if(step==n+1){//查到n+1就该退出
		ans=max(ans,cnt);//存最大值
		return ;
	}
	if(a[step]==x)	dfs(x+1,step+1,cnt+1);//找到了就要
	else	dfs(x,step+1,cnt);//否则往后查
}
int main(){
	freopen("increase.in","r",stdin);//要用freopen
	freopen("increase.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	dfs(1,1,0);//从一开始查,先找1,长度初始化
	cout<<ans;
	return 0;
}
原文