?? 最長(zhǎng)不下降子序列.cpp
字號(hào):
// 最長(zhǎng)不下降子序列.cpp : Defines the entry point for the console application.
/*
MaxLen (k)表示以ak做為“終點(diǎn)”的最長(zhǎng)上升子序列的長(zhǎng)度
MaxLen (k) = Max { MaxLen (i):1<i < k 且ai < ak且k≠1 } + 1
*/
//
#include "stdafx.h"
#include <stdio.h>
#include <memory.h>
#define MAX_N 1000
int b[MAX_N +10];
int aMaxLen[MAX_N +10];
main(){
int N;
scanf("%d", &N);
for( int i=1;i<=N;i++ )
scanf("%d", &b[i]);
aMaxLen[1] = 1; //以1為終點(diǎn)的最長(zhǎng)上升子序列的長(zhǎng)度為1
for( i = 2; i <= N; i++ )
{
//每次求以第i個(gè)數(shù)為終點(diǎn)的最長(zhǎng)上升子序列的長(zhǎng)度
int nTmp = 0; //記錄滿足條件的,第i個(gè)數(shù)左邊的上升子序列的最大長(zhǎng)度
for( int j = 1; j < i; j ++ )
{ //察看以第j個(gè)數(shù)為終點(diǎn)的最長(zhǎng)上升子序列
if( b[i] > b[j] ) {
if( nTmp < aMaxLen[j] )
nTmp = aMaxLen[j];}
}
aMaxLen[i] = nTmp + 1;}
int nMax = -1;
for( i = 1; i <= N; i ++ )
if( nMax < aMaxLen[i])
nMax = aMaxLen[i];
printf("%d\n", nMax);
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -