本题为求最长上升子序列的典型例题,是求最长下降子序列,我比较笨,用了n^2的解法
Description
昨天是平安夜,圣诞老人从房顶上的烟囱里爬到小朋友床边把礼物送给梦乡中的小朋友,但是今年的圣诞老人是处女座的,他有很严重的强迫症,他从一条街的一端开始,每次送礼物进的烟囱都不能比之前进的烟囱高,而且他还想要送出最多的礼物。
Input
输入数据只有一行,该行包含若干个数据,表示一条街上的烟囱高度(烟囱最多有 20 枚,高度h≤1000000)。
Output
圣诞老人最多送出多少礼物。
Sample Input
315 199 155 301 215 170 150 25
Sample Output
6
#include <cstdio>
#include <iostream>
#include <cmath>
#include <
string>
#include <cstring>
#include <algorithm>
using namespace std;
int heihgt[
28], dp[
28], len =
1;
int main()
{
int r, miao =
0, sum =
0;
while(cin >>
r)
{
heihgt[miao++] =
r;
}
for(
int i =
0; i<miao; i++
)
{
dp[i] =
1;
for(
int j = i-
1; j >=
0; j--
)
{
if(heihgt[j] >= heihgt[i] && dp[j]+
1 >= dp[i])dp[i] = dp[j]+
1;
}
if(dp[i]>len)len =
dp[i];
}
printf("%d\n", len);
return 0;
}
转载于:https://www.cnblogs.com/RootVount/p/10360675.html