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

题意:给出3种操作:增加/减少一个a,代价是x,把a的个数翻倍,代价是y,要求让一个空字符串变成有n个a的字符串,求最小代价

题解:dp,从最终状态往前推,当当前长度len为奇数时,可以由len+1,len-1转移得到,当len为偶数时,可以由len+1,len-1,len/2转移得到,最后得到dp[0]既是答案

#include<cstdio>
#include<queue>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 2e7 + 5;
typedef long long LL;
typedef pair<int,LL> PII;
LL d[maxn];
bool vis[maxn];
int main(){
	int n;
	LL x,y;
	scanf("%d%I64d%I64d",&n,&x,&y);
	/*
	priority_queue<PII,vector<PII>,greater<PII> >q;
	q.push(PII(n,0));
	*/
	queue<int>q;
	q.push(n);
	memset(d,0x3f,sizeof(d));
	LL ans=d[0];
	d[n]=0;
	while(!q.empty()){
		int p=q.front();q.pop();
		vis[p]=0;
		if(p==0) continue;
		int tmp;
		if(p%2){
			if(p<2*n&&d[p+1]>d[p]+x){
				tmp=p+1;
				d[tmp]=d[p]+x;
				if(!vis[tmp]) q.push(tmp),vis[tmp]=1;
			}
			if(d[p-1]>d[p]+x){
				tmp=p-1;
				d[tmp]=d[p]+x;
				if(!vis[tmp]) q.push(tmp),vis[tmp]=1;
			}
		}
		else{
			
			if(d[p/2]>d[p]+y){
				tmp=p/2;
				d[tmp]=d[p]+y;
				if(!vis[tmp]) q.push(tmp),vis[tmp]=1;
			}
			if(p<2*n&&d[p+1]>d[p]+x){
				tmp=p+1;
				d[tmp]=d[p]+x;
				if(!vis[tmp]) q.push(tmp),vis[tmp]=1;
			}
			if(d[p-1]>d[p]+x){
				tmp=p-1;
				d[tmp]=d[p]+x;
				if(!vis[tmp]) q.push(tmp),vis[tmp]=1;
			}
		}
	}
	printf("%I64d\n",d[0]); 
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值