codeforces 710E Generate a String(dp)

E. Generate a String
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard 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 nx and y (1 ≤ n ≤ 1071 ≤ 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



题意:要输入n个字符'a',有两种操作,一种是输入或删除一个'a',耗时x;另一种是把当前的整个文本复制粘贴,耗时y。求最少时间。




考虑dp,用d[i]表示输入n个字符的最少时间,其边界条件d[0]=0。

当i为偶数时,

对于任意的i>=2,我们考虑最后一步操作,最后一步操作如果是输入一个字符,那么就有d[i]=d[i-1]+x。

最后一步操作如果是复制粘贴,那么就有d[i]=d[i/2]+y。

即d[i]=min(d[i-1]+x,d[i/2]+y)

当i为奇数时,如果最后一步操作是输入一个字符,同样有d[i]=d[i-1]+x。

但是如果最后一步是复制粘贴的话,因为i是奇数,所以就没办法直接复制粘贴了。

这里有两种选择,

一种是从d[i/2]的状态复制粘贴一遍,然后再插入一个字符。

即d[i]=d[i/2]+x+y

另一种是从d[i/2+1]的状态复制粘贴一遍,然后再删除一个字符。

即d[i]=d[i/2+1]+x+y

所以当i为奇数时,有d[i]=min(d[i-1]+x,d[i/2]+x+y,d[i/2+1]+x+y)




#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e7+10;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll d[maxn];
int main()
{
	ll n,x,y;
	scanf("%I64d%I64d%I64d",&n,&x,&y);
	d[0]=0;
	for(int i=1;i<=n;i++)
	{
		if(i&1) d[i]=min(d[i-1]+x,min(d[i/2]+y+x,d[i/2+1]+y+x));
		else d[i]=min(d[i-1]+x,d[i/2]+y);
	}
	printf("%I64d\n",d[n]);
}


















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值