1254: Redraiment的走法
Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 3 Solved: 2
[ Submit][ Status][ Web Board]
Description
Redraiment是个聪明人,总是以奇怪的思考方法思考问题,但不知道为什么,他的解答总是最最巧妙,我们隆重地称他为诡异人! 有一天Jesse不经意中发现,诡异人的走路方法很特别,于是特别关注了他的走路规则。他发现诡异人总是往高处走,但走的步数总是最多,不知道为什么?你能替Jesse研究研究他最多走的步数吗? 发现了你也会是个聪明人!^_^
Input
There has several test cases. Each case start with an integer n(0 < n ≤10000), then follows n lines.Each line has an integer h( 1 ≤ h ≤ 100),which represents the height of the place.
Output
For each case output a line with the max number of the steps he can go .
Sample Input
5
1 2 3 4 5
6
2 5 1 5 4 5
Sample Output
5
3
HINT
Example:
6个点的高度各为 2 5 1 5 4 5
如从第1格开始走,最多为3步, 2 4 5
从第2格开始走,最多只有1步,5
而从第3格开始走最多有3步,1 4 5
从第5格开始走最多有2步,4 5
Source
典型的动态规划中的一个非常经典的例子,最长上升子序列问题!
#include <stdio.h>
#define max 10000
int main()
{
int n,a[max],amaxlen[max],len,nlen,i,j;//a[]保存数据而用的,amaxlen[]是保存每一个数之前的最长长度!
while (scanf("%d",&n)!=EOF)
{
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
amaxlen[1]=1;
for (i=2;i<=n;i++)
{
len=0;
for (j=1;j<i;j++)
{
if (a[i]>a[j])
{
if (len<amaxlen[j])
{
len=amaxlen[j];
}//这个比较经典,就是先求出比a[i]小的最长子序列,最后再加一就OK了!
}
}
amaxlen[i]=len+1;
}
nlen=-1;
for (i=1;i<=n;i++)
{
if (nlen<amaxlen[i])
{
nlen=amaxlen[i];
}
}//比较每一个数的最长子序列,最后求得最大值!
printf("%d\n",nlen);
}
}
从这一题,我也看出了动态规划的思想,就是划分子问题,求得每一个子问题的最优解,最终得到整个问题的最优解!感觉非常棒!