280K 63MS GCC 600B 2009-01-10 23:08:06

Pku1887----Testing the CATCHER (经典动态规划题:最长下降子序列),,,,,捎带pku2533---Longest Ordered Subsequence哭死了,这道题目竟然错了十几次之多。

是一道很简单的题目:最长下降子序列问题。。。。。

刚开始错在数组开的太小,导致runtime error,

其次,误以为最后一个元素里存放的就是最大值,根本就没经过大脑嘛。。。

接下来,发现自己忘了元素还有是1个的情况。。。。

最后,更可恶的是,我把上面的代码改了,加了max,输出结果的时候还是用了原来那个。。。。。。。。。。

我真郁闷的类,wrong了这么多次,唉。。。。。。。。。。。。。。。就给自己买个教训吧。好大的代价啊。。

不说了,核心代码是:

 for(i=2;i<n;i++){
  best[i] = 1;
  for(j=1;j<i;j++){
   if(a[j]>=a[i]&&best[j]+1>best[i])
    best[i]=best[j]+1;
  }
  if(best[i]>max)
    max=best[i];
 }

代码如下:

 

 input()
{
    scanf("%d",&a[1]);
    
if(a[1]==-1)
        
return 0;
    
else
    {
        n 
= 2;
        
while(scanf("%d",&a[n]),a[n]!=-1)
            n
++;
        
return 1;
    }
}
void process()
{
    
int i,j,max;
    
    best[
1= 1;max=1;
    
for(i=2;i<n;i++){
        best[i] 
= 1;
        
for(j=1;j<i;j++){
            
if(a[j]>=a[i]&&best[j]+1>best[i])
                best[i]
=best[j]+1;
        }
        
if(best[i]>max)
                max
=best[i];
    }
    printf(
"Test #%d:\n  maximum possible interceptions: %d\n\n",caseno,max);
}
int main()
{
    
while(input()){
        caseno
++;
        process();
    }
    
return 0;
}

 

pku2533:

 

 input()
{
    int i;
    scanf(
"%d",&n);n++;
    
for(i=1;i<n;i++)
        scanf(
"%d",&a[i]);
}
void process()
{
    
int i,j,max;
    
    best[
1= 1;max=1;
    
for(i=2;i<n;i++){
        best[i] 
= 1;
        
for(j=1;j<i;j++){
            
if(a[j]<a[i]&&best[j]+1>best[i])
                best[i]
=best[j]+1;
        }
        
if(best[i]>max)
                max
=best[i];
    }
    printf(
"%d\n",max);
}
int main()
{
    
        input();
        process();
    
    
return 0;
}

相关文章:

  • 2022-01-28
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-06
  • 2022-12-23
  • 2021-08-28
  • 2022-02-09
  • 2022-02-02
  • 2022-12-23
  • 2021-07-03
相关资源
相似解决方案