http://codeforces.com/contest/710/problem/E
给n,x,y,
要求生成n个‘a’
初始文本编辑器空白。
每次插入一个a,花费x,删除一个a,花费x,
每次复制当前全部文本一次,花费y,
求最小花费生成y
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.
对于 dp[i],如果i是奇数,则i的来源只能是通过dp[i-1]加1得到,或者dp[i/2+1]复制一次,再减1得到。
所以dp[i]=min(dp[i-1]+x,dp[i/2+1]+y+x);
如果i是偶数,则同样,只能是通过dp[i-1]加1得到,或者dp[i/2]复制一次得到。
dp[i]=min(dp[i-1]+x,dp[i/2]+y)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
double eps=0.000001;
typedef long long ll;
const int N=(1e7)+50;
ll dp [N];
int main()
{
// freopen("in.txt","r",stdin);
ll n ,x ,y;
cin>>n>>x>>y;
dp[0]=0,dp[1]=x;
for (int i=2;i<=n;i++)
{
if (i%2)
dp[i]=min(dp[i-1]+x,dp[i/2+1]+y+x);
else dp[i]=min(dp[i-1]+x,dp[i/2]+y);
}
printf("%lld\n",dp[n]);
return 0;
}
本文解析了CodeForces竞赛中一道关于文本编辑操作的题目,通过动态规划的方法找到生成特定数量字符'a'所需的最小成本。
271

被折叠的 条评论
为什么被折叠?



