codeforces Educational Codeforces Round 16-E(DP)

题目链接:http://codeforces.com/contest/710/problem/E

题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍),问要获得长度为n的串所需最少的时间。

思路:dp[i]表示获得长度为i的串所需要的最短时间,分i为奇数和偶数讨论。

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+3;
typedef long long ll;
ll dp[N];
int main()
{
    int n,x,y;
    scanf("%d %d %d",&n,&x,&y);
    for(int i=1;i<=n;i++)
    {
        if(i&1)
            dp[i]=min(dp[i-1]+x,dp[i/2+1]+x+y);
        else
            dp[i]=min(dp[i-1]+x,dp[i/2]+y);
    }
    printf("%I64d
",dp[n]);
    return 0;
}



原文地址:https://www.cnblogs.com/westwind1005/p/5975208.html