POJ1836 Alignment DP

Problem Address:http://poj.org/problem?id=1836

 

还是一道DP题,弄了半个晚上没弄好,没办法,去睡觉。

 

早上想早点起,结果错过了闹钟。

 

看了一下,居然就把错误找出来了。

 

提交,AC。好自在~

 

冲碗红烧牛肉泡面吃一下,难为了昨晚睡觉的时候肚子还咕咕叫,估计是费脑力了。

 

 

 

思路是对每一个数向两边求递减和递增的最长序列。不过要注意存在中间相等的情况。

 

以下贴代码:

 

#include <iostream>
using namespace std;
double s[1005],dp[1005];
struct end
{
 int ct;
 int m;
};
end ddp(int len)
{
 int i,j,ct;
 ct = 0;
 for (i=0; i<len; i++)
 {
  for (j=0; j<ct; j++)
  {
   if (dp[i]>=dp[j])
   {
    dp[j] = dp[i];
    break;
   }
  }
  if (j==ct)
  {
   dp[j] = dp[i];
   ct++;
  }
 }
 end e;
 e.ct = ct;
 e.m = dp[0];
 return e;
}
int main()
{
 int n,total,i,j,k,max;
 end L,R;
 scanf("%d", &n);
 for (i=0; i<n; i++)
 {
  scanf("%lf", &s[i]);
 }
 max = 0;
 for (k=0; k<n; k++)
 {
  for (i=k-1,j=0; i>=0; i--)
  {
   if (s[i]<=s[k]) dp[j++]=s[i];
  }
  if (j!=0)
  {
   L = ddp(j);
  }
  else
  {
   L.ct = 0;
   L.m = -1;
  }
  for (i=k+1,j=0; i<n; i++)
  {
   if (s[i]<=s[k]) dp[j++]=s[i];
  }
  if (j!=0)
  {
   R = ddp(j);
  }
  else
  {
   R.ct = 0;
   R.m = -1;
  }
  if (L.m==s[k] && R.m==s[k]) total = L.ct+R.ct;
  else total = L.ct+R.ct+1;
  if (total>max) max = total; 
 }
 printf("%d/n", n-max);
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值