SDNU1292.圣诞老人【动态规划】

来源:互联网 发布:label mx 破解软件 编辑:程序博客网 时间:2024/04/19 10:46
http://www.acmicpc.sdnu.edu.cn/Problem.aspx?pid=1292

Description


昨天是平安夜,圣诞老人从房顶上的烟囱里爬到小朋友床边把礼物送给梦乡中的小朋友,但是今年的圣诞老人是处女座的,他有很严重的强迫症,他从一条街的一端开始,每次送礼物进的烟囱都不能比之前进的烟囱高,而且他还想要送出最多的礼物。


Input


输入数据只有一行,该行包含若干个数据,表示一条街上的烟囱高度(烟囱最多有 20 枚,高度h≤1000000)。
Output


圣诞老人最多送出多少礼物。


Sample Input


315 199 155 301 215 170 150 25
Sample Output


6

动态规划问题。


#include<iostream>using namespace std;int main(){    int a[22],b[22]; a[0]=10000001;   b[0]=0;   int x=1,y;    while(cin>>y)  {       a[x]=y;   x++;  }/*到达第i个烟囱最多能送出多少礼物,用数组记录*/for(int i=1;i<x;i++)  {      int max=-1;    for(int j=i-1;j>=0;j--)   {          if(a[i]<=a[j]&&max<b[j])    {               max=b[j];       }           }       b[i]=max+1;  }    int max2=0;  for(int i=1;i<x;i++)  {      if(b[i]>max2)    {max2=b[i];}   }cout<<max2<<endl;return 0;}


0 0
原创粉丝点击