codeforces 710E Generate a String [dp]【动态规划】

题目链接:http://codeforces.com/problemset/problem/710/E
————————————————-.
E. Generate a String
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
zscoder wants to generate an input file for some programming competition problem.

His input is a string consisting of n letters ‘a’. He is too lazy to write a generator so he will manually generate the input in a text editor.

Initially, the text editor is empty. It takes him x seconds to insert or delete a letter ‘a’ from the text file and y seconds to copy the contents of the entire text file, and duplicate it.

zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters ‘a’. Help him to determine the amount of time needed to generate the input.

Input
The only line contains three integers n, x and y (1 ≤ n ≤ 107, 1 ≤ x, y ≤ 109) — the number of letters ‘a’ in the input file and the parameters from the problem statement.

Output
Print the only integer t — the minimum amount of time needed to generate the input file.

Examples
input
8 1 1
output
4
input
8 1 10
output
8
————————————–.
题目大意 :
就是从0开始 每次可以+1或-1或*2 +-操作花费x *2操作花费y 问你达到n的最小花费是多少?

解题思路 :
就是DP
DP 的时候只要从0遍历到n即可 因为题目的数据范围是1e7 所以并不会超时
转移的时候一共决策三种状态 就是
1.由i-1 转移过来的
2.由i/2 转移过来的 这时候要区分下奇数还是偶数
——— 偶数很简单了 只要决策i 与 i/2 的状态就行了
——— 奇数的话 那么一定是前一个数*2+1或者*2-1达到的状态 由于遍历的时候已经判断了+1的情况 所以 这里只需考虑-1的情况就行 所以决策的事i 与 i/2+1

然后输出下结果就好了

附本题代码
——————————————-.

#include <bits/stdc++.h>

using namespace std;

#define LL long long int
const int M = 1e7+7;
LL dp[M];
int main()
{
    ios::sync_with_stdio(false);
    LL n,x,y;
    while(cin>>n>>x>>y)
    {
        dp[0]=0,dp[1]=x;
        for(int i=2;i<=n;i++)
        {
            dp[i]=dp[i-1]+x;
            if(i&1) dp[i]=min(dp[i],dp[i/2+1]+y+x);
            else    dp[i]=min(dp[i],dp[i/2]+y);
        }
        printf("%I64d\n",dp[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值