题目链接:

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 ≤ 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

题意:

现在添加一个或者删除一个a需要x,复制一次需要y,问正好有n个a至少需要多少时间;

思路:

dp[i]表示正好i个a需要的最少时间,i为偶数的时候dp[i]=min{dp[i-1]+x,dp[i+1]+x,dp[i/2]+y}
i为奇数的时候dp[i]=min{dp[i-1]+x,dp[i+1]+x};
这样写的状态转移方程没法转移啊,把这些式子组合考虑一下可以知道:
当确定了i的dp[i]的时候,可以得到dp[i*2]的估计值为dp[i]+y;这就是得到了偶数的估计值,
所以i%2==1时dp[i]=min{dp[i-1]+x,dp[i+1]+x};i%2==0时dp[i]=min{dp[i-1]+x,dp[i/2]+y}; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e7+10;
const int maxn=1e3+20;
const double eps=1e-12; LL dp[N],x,y;
int n;
int main()
{ read(n);read(x);read(y);
dp[1]=x;dp[2]=x+y;
for(int i=2;i<=n;i++)
{
if(i&1)dp[i]=min(dp[i-1]+x,dp[i+1]+x);
else dp[i]=min(dp[i-1]+x,dp[i/2]+y);
dp[2*i]=dp[i]+y;
}
cout<<dp[n]<<endl;
return 0;
}

  

codeforces 710E E. Generate a String(dp)的更多相关文章

  1. Codeforces 710 E. Generate a String (dp)

    题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. d ...

  2. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  3. CodeForces 710E Generate a String (DP)

    题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...

  4. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

  5. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  6. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  7. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  8. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  9. codeforces 710E Generate a String(简单dp)

    传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...

随机推荐

  1. CodeBlocks VS2015编译环境设置

    1. 菜单 Settings --> Compiler... 2. 设置vs 的安装路径

  2. 【GOF23设计模式】解释器模式 & 访问者模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter  2.访问者模式Visitor 

  3. javascript函数中的三个技巧【一】

    在学习javascript中,函数是非常重要的,现在我来谈谈对函数的理解以及在工作和用法中的一些技巧 技巧一. [作用域安全的构造函数] 构造函数其实就是一个使用new操作调用的函数 function ...

  4. ASP.NET数据绑定技术

    1.DataBinder.Eval()方法 DataBinder.Eval()方法是ASP.NET框架支持的一个静态方法,用来计算Late_Bound(后期绑定)数据绑定表达式,并随时将结果转换为字符 ...

  5. css三角形的实现

    实底三角形: <html> <head> <title></title> <style type="text/css"> ...

  6. Oracle执行计划与统计信息的一些总结

    [日期:2011-08-05]来源:Linux社区  作者:wangshengfeng1986211[字体:大 中 小] 2010-07-01 15:03 1.SET AUTOTRACE ON EXP ...

  7. 桥牌笔记:Skill Level 4 D8

    西拿黑桃K.A后,转攻小方块. 看来只有一个小红桃失张了.飞方块没有必要冒险.但将牌分布4-0时会有点麻烦.

  8. [leetcode] Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

  9. 主程序底部TabBar功能跟登录页面布局

    1:主程序底部TabBar的功能实现 效果图: 主要代码如下: - (UITabBarController*)setRootVC:(BOOL)bShowCart { //创建一个子控制器 用于显示当前 ...

  10. System.Data.Entity 无法引用的问题

    最近刚学MVC,跟着网上的博客学习,发现代码中有这样一句: using System.Data; using System.Data.Entity; 我项目引用的时候,也引用了System.Data. ...