CUGB 1009 DP简单动态规划,此题一开始就结了很久,WA了9次对于刚开始学DP的我打击很大,最后发现原来是方法弄错了,题意没完全弄懂!看了大牛的文章才知道,差距啊!还得好好努力。

#include<stdio.h>

int main()
{
    int i, j, N, temp, max;
    int high[102],dp1[102],dp2[102];
   
     while(scanf("%d", &N) != EOF)
     {
        scanf("%d", &high[1]);
        for(i=2; i<=N; i++)
        {
           scanf(" %d",&high[i]);
        }
       
        dp1[1] = 1;
        for (i=2; i<=N; i++)
        {
            temp = 0;
          for (j=1; j<i; j++)
          {
            if ((high[i]>high[j]) && (temp<dp1[j]+1))
              temp = dp1[j];
          }
          dp1[i] = temp + 1;
        }
       
         dp2[N] = 1;
         for (i=N-1; i>0; i--)
         {
            temp = 0;
           for (j=N; j>i; j--)
           {
            if ((high[i]>high[j]) && (temp<dp2[j]+1))
              temp = dp2[j];
          }
          dp2[i] = temp + 1;
        }
       
        max = dp1[1] + dp2[1];
        for(i=2; i<=N; i++)
        {
          if(max < dp1[i]+dp2[i])
            max = dp1[i] + dp2[i];
        }    
             
        printf("%d\n", N+1-max);
    } 
    return 0;
}  

       

posted @ 2011-08-16 17:23  zhongya  阅读(160)  评论(0编辑  收藏  举报