某一维线性dp

线性dp

题目:

Mr. Wang need to get a string which consists of n letters of ‘k’ for his work.
Initially the string is empty.He has two methods to modify the string.He can cost x seconds to insert or delete a letter ‘k’, or he can cost
y seconds to copy the contents of the entire string, and duplicate it.He can use the two methods any times.
Mr. Wang wants to know the minimum time it costs to get the string of exactly n letter ‘k’. Can you make a programm to help him get the answer?

原题网站:http://dutacm.club:7217/codesheaven/problem.php?id=1089

题意:

就是想办法把一个数通过加减乘从0变成n,其中加减1的代价为x,乘2倍的代价为y,想办法让总代价最少。

思路

是一个线性dp,因为虽然可加可减,但是对于一个数字n1,最多由(n1+1)-1得来,如果由(n/2+1)乘2再得来,肯定比由(n/2+1)减1再乘2费用要大,所以直接线性处理就可以了,对于一个偶数n,dp[n]=min(dp[n-1]+x,dp[n/2]+y),对于一个奇数n,dp[n]=min(dp[n-1]+x,dp[(n+1)/2]+x+y)。
`#include

include

using namespace std;
typedef long long LL;
const int N=10000100;
LL dp[N],x,y;
int n;
int main()
{
while(scanf(“%d%lld%lld”,&n,&x,&y)!=EOF)
{
dp[0]=0;
for(int i=1;i<=n;++i)
{
dp[i]=dp[i-1]+x;
if(i&1)
dp[i]=min(dp[i],dp[(i+1)>>1]+x+y);
else
dp[i]=min(dp[i],dp[i>>1]+y);
}
printf(“%lld\n”,dp[n]);
}
return 0;
}
`

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值