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
思路:这个题目要分奇偶讨论。
如果n是偶数,有两种转换形式
①n-1->n
②n/2->n(n/2直接复制到n,花费y)
如果n是奇数,有三种转换形式
①n-1->n
②n/2->n(n的一半复制到n-1花费y,然后还少一个,采用增加一个的形式,花费x)
③n/2+1->n(n/2+1复制到n+2花费y,多了一个,采用减少一个的形式,花费x)
状态转移方程:

if(i&1) dp[i]=min(dp[i-1]+x,min(dp[i/2]+x+y,dp[i/2+1]+x+y));
else dp[i]=min(dp[i-1]+x,dp[i/2]+y);

代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e7+100;
ll n,x,y;
ll dp[maxx];int main()
{scanf("%lld%lld%lld",&n,&x,&y);memset(dp,-1,sizeof(dp));dp[0]=0;dp[1]=x;for(int i=2;i<=n;i++){if(i&1) dp[i]=min(dp[i-1]+x,min(dp[i/2]+x+y,dp[i/2+1]+x+y));else dp[i]=min(dp[i-1]+x,dp[i/2]+y);}cout<<dp[n]<<endl;return 0;
}

努力加油a啊,(o)/~

Generate a String CodeForces - 710E(dp)相关推荐

  1. Codeforces 1144G(dp)

    据说这题是种dp的套路?然后被我国红名神仙(南大Roundgod)贪心了,不过思路上非常相近了,故而可贪吧. 设的dp[i][0]是:如果把第i个数放在上升序列里了,那么下降序列结尾的那个最大是多少: ...

  2. Minimum Ternary String CodeForces - 1009B(思维)

    You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...

  3. 求三角形最大面积(DP)

    求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...

  4. LeetCode 编辑距离 II(DP)

    1. 题目 给你两个单词 s 和 t,请你计算出将 s 转换成 t 所使用的最少操作数. 你可以对一个单词进行如下两种操作: 删除一个字符 替换一个字符 注意: 不允许插入操作 题目保证有解 示例: ...

  5. LeetCode 1406. 石子游戏 III(DP)

    1. 题目 Alice 和 Bob 用几堆石子在做游戏.几堆石子排成一行,每堆石子都对应一个得分,由数组 stoneValue 给出. Alice 和 Bob 轮流取石子,Alice 总是先开始.在每 ...

  6. LeetCode 72. 编辑距离(DP)

    1. 题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示 ...

  7. 2019年杭电多校第一场 1001题blank(DP)HDU6578

    2019年杭电多校第一场 1001题blank(DP)HDU6578 解决思路,开一个DP数组来存储0 1 2 3四个字符最后出现的位置,并且在DP中已经==排好序==. DP开四维,DP[i][j] ...

  8. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  9. 0-1背包问题(DP)-超有趣版

    0-1背包问题(DP)-超有趣版 文章目录 0-1背包问题(DP)-超有趣版 一. 0-1背包问题 二. 0-1背包问题分析 三.0-1背包问题--DP算法求解 四.0-1背包问题--DP算法深入分析 ...

最新文章

  1. AI 行业寒冬犹在:融资规模不足巅峰时期一半,上市潮将伴随倒闭潮
  2. GitLab创建空仓库后拉取并上传文件遇到的系列问题
  3. 【rabbitmq】Authentication failed (rejected by the remote node), please check the Erlang cookie
  4. html使用js的变量_JS变异小技巧:使用JavaScript全局变量绕过XSS过滤器
  5. Junit_@Before@After
  6. python中的doc_在windows 10上读取python中的.doc文件
  7. iPad连android热点掉线,苹果终于承认,iOS 13有这个问题,网络断连的原因找到了...
  8. 股市大涨是不是楼市就要跌了?
  9. Bootstrap 面包屑导航
  10. linux 7 改网卡名称,CentOS7修改网卡名称
  11. MongoDB工具MagicMongoDBTool使用介绍(一) -- 简单MongoDB入门
  12. C语言连接Oracle数据库
  13. 生信技能树R语言学习
  14. Groovy 教程系列(一)-- Groovy 入门
  15. Android简易本地音乐播放器,简单实现Android本地音乐播放器
  16. 百度地图api使用时标注图标显示不出来
  17. python中index什么意思_Python中index()和seek()的用法(详解)
  18. python用谷歌内核制作浏览器_用cef Python打造自己的浏览器
  19. docker执行权限问题Got permission denied while trying to connect to the Docker daemon socket
  20. vs2017\vs2019 VGG19处理cifar-10数据集的TensorFlow实现

热门文章

  1. c语言中strcmp作用,C语言中strcmp的实现原型
  2. 信息收集——Web目录扫描
  3. python正则匹配日期2019-03-11_都2019年了,正则表达式为啥还是这么难?这里的Python学习教程教你搞定!...
  4. 不能装载文档控件。请在检查浏览器的选项中检查浏览器的安全设置_Excel基础—文件菜单之设置信息...
  5. java银行叫号模拟系统_Java 模拟银行叫号机
  6. iOS 开发和部署过程概述
  7. android异步网络连接开源:Android Asynchronous Http Client
  8. java 英文分词器使用代码_java文本英文分词
  9. invalid signature_php,微信开发JSSDK遇到的问题 config:invalid signature
  10. Citrix Provisioning Services:Windows 10 VDA计算机的登录性