1259:【例9.3】求最长不下降序列

http://ybt.ssoier.cn:8088/problem_show.php?pid=1259

/*
【例3】求最长不下降序列03_AC
1259:【例9.3】求最长不下降序列
http://ybt.ssoier.cn:8088/problem_show.php?pid=1259
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int a[maxn],i,j,k,maxx,c[maxn],num[maxn],b[maxn];
 //数组b记录从i位置到n的最长不下降序列长度,
 //数组c表示从i位置开始最长不下降序列的下一个位置,
 //若c[i]==0,则表示后面没有链接项。
int main()
{
	//freopen("x.in","r",stdin);
	//freopen("x.out","w",stdout);
	int n;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		//输入数组,并将b和c初始化
		cin>>a[i];              
		b[i]=1;
		c[i]=0;
	}
 
	//求最长不下降序列
	for(i=1;i<=n;i++)        
	{
		maxx=0;
		k=0;
		//顺推 
		for(j=1;j<=i-1;j++)
			if(a[j]<=a[i] && b[j]>maxx)
			{
			   maxx=b[j];
			   k=j;
			}
		if(maxx>0)
		{
			b[i]=maxx+1;
			c[i]=k;
		}
	}
    
	//求最长不下降序列最后位置
	k=1;
	for(j=1;j<=n;j++)         
		if(b[j]>b[k]) k=j;
	
	cout<<"max="<<b[k]<<endl;
	
	i=0;    
	//因为不是倒着搜索的,我只能将数组fu给另一个,
	//然后倒着输出
	while(k!=0)              
	{
	    num[i]=a[k];
	    k=c[k];
	    i++;
	}
	
	for(j=i-1;j>=0;j--)
	{
		cout<<num[j]<<" ";
		//cout<<"num["<<j<<"]="<<num[j]<<" ";
	}
	   
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}
/*
作业:
1.1096:数字统计
http://ybt.ssoier.cn:8088/problem_show.php?pid=1096

2.1949:【10NOIP普及组】数字统计
http://ybt.ssoier.cn:8088/problem_show.php?pid=1949

3.1.5编程基础之循环控制_25求特殊自然数
http://noi.openjudge.cn/ch0105/25/

4.1281:最长上升子序列
http://ybt.ssoier.cn:8088/problem_show.php?pid=1281

5、1285:最大上升子序列和
http://ybt.ssoier.cn:8088/problem_show.php?pid=1285

6、1288:三角形最佳路径问题
http://ybt.ssoier.cn:8088/problem_show.php?pid=1288 
*/ 

 

 

 

 

 


 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dllglvzhenfeng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值