Generate a String

题目链接:

http://codeforces.com/contest/710/problem/E

Description


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.

Sample Input


```
8 1 1
8 1 10
```

Sample Output


```
4
8
```


##题意:

数num初始时为0,可以用x的花费加一或减一,可以用y的花费乘二,求最小花费变成N.


##题解:

规模这么大,要么是找规律,要么是O(n). 最短路什么的肯定不行了.
这里用我为人人的dp来解决:dp[i]: 达到i的最小花费.
枚举i:对于i有三种拓展方向(加一 减一 乘二).
如果直接进行这三种拓展,肯定存在问题:因为存在减一的操作,所以i的最小值可能会由i+1得来.
考虑一个点i,如果它的最小花费是由比i大的点拓展而来的,那么这个点一定是i+1,且它是一个偶数.
证明:首先,因为比i大只能减一操作,所以最小的一定是i+1.
其次,i+1这个数的来源有三种i,i+2,i/2. 肯定不能是i(否者就重复走了),如果是i+2,那么违背上一个条件(i+2连续减两次到达i),所以一定是i/2通过乘二操作得到i.
综上,我们只要在每次更新2*n时,同时更新2*n-1,即可保证每次枚举到i时,dp[i]都是最小.
这题也可以写成"人人为我"的形式,见代码注释.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 10000100
#define mod 100000007
#define inf 0x3f3f3f3f3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL dp[maxn];

int main(int argc, char const *argv[])

{

//IN;

int n; LL x,y;
while(scanf("%d %I64d %I64d", &n,&x,&y) != EOF)
{
memset(dp, inf, sizeof(dp));
dp[1] = x;
for(int i=1; i<=n; i++) {
dp[i+1] = min(dp[i+1], dp[i] + x);
if(i*2 < maxn) {
dp[i*2] = min(dp[i*2], dp[i] + y);
dp[i*2-1] = min(dp[i*2-1], dp[i*2] + x);
}
} /*
"我为人人"
for(int i=1; i<=n; i++) {
dp[i] = min(dp[i-1]+x, dp[(i+1)/2]+y+(i%2)*x);
}
*/ printf("%I64d\n", dp[n]);
} return 0;

}

Educational Codeforces Round 16 E. Generate a String (DP)的更多相关文章

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

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

  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. Educational Codeforces Round 20 E - Roma and Poker(dp)

    传送门 题意 Roma在玩一个游戏,一共玩了n局,赢则bourle+1,输则bourle-1,Roma将会在以下情况中退出 1.他赢了k个bourle 2.他输了k个bourle 现在给出一个字符串 ...

  4. CodeForces - 710E Generate a String (dp)

    题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...

  5. Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)

    Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...

  6. Educational Codeforces Round 22 B. The Golden Age(暴力)

    题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...

  7. Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)

    第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...

  8. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...

  9. D Merge Equals Educational Codeforces Round 42 (Rated for Div. 2) (STL )

    D. Merge Equals time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

随机推荐

  1. C语言Ⅰ博客作业02

    1. 这个作业属于哪个课程 C语言程序设计Ⅰ 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/8656 我在这个课程 ...

  2. java中的继承关系

    1.定义 java中的继承是单一的,一个子类只能拥有一个父类:java中所有类的父类是java.lang.Object,除了这个类之外,每个类只能有一个父类: 而一个父类可以有多个子类,可以被多个子类 ...

  3. 用了 10 多年的 Tomcat 居然有bug !

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 为了解决分布式链路追踪的问题,我们引入了实现OpenTracing的Jaeger来实现.然后我们为SpringBoot框架写 ...

  4. java 异常体系详细介绍

    一.异常概述与异常体系结构 异常:在Java语言中,将程序执行中发生的不正常情况称为"异常".(开发过程中的语法错误和逻辑错误不是异常). Java把异常当作对象来处理,并定义一个 ...

  5. MyBatis二级缓存的笔记及记录

    一.什么是二级缓存: 由于一级缓存是一次性的.临时的:每个会话都会创建一个新的:多个会话之间是不能共享的: 二级缓存用于解决一级缓存的不足:每一个“namespace”都会对应一个二级缓存:执行查询的 ...

  6. Qt 遍历不规则树的节点

    在使用Qt的GraphicsScene作图时,遇到类似这样的需求:在scene中创建节点类似下图, 现在我要把每个节点的txt保存到xml文件中,结构为 <?xml version='1.0' ...

  7. 小白学Python——Matplotlib 学习(1)

    众所周知,通过数据绘图,我们可以将枯燥的数字转换成容易被人们接受的图表,从而让人留下更加深刻的印象.而大多数编程语言都有自己的绘图工具,matplotlib就是基于Python的绘图工具包,使用它我们 ...

  8. P2586 [ZJOI2008]杀蚂蚁

    传送门 快乐模拟,修身养性 代码长度其实还好,主要是细节多 只要知道一些计算几何基础知识即可快乐模拟,按着题目要求一步步实现就行啦 注意仔细读题,蚂蚁每 $5$ 秒乱走一次的时候是只要能走就走了,不一 ...

  9. 清理Windows图标缓存 | 懒人屋

    原文:清理Windows图标缓存 | 懒人屋 文章背景 这是一个抄袭的文章,原文在参考资料中 运行环境 操作系统:Windows 10 x64(1903) 清理脚本 @echo off rem 关闭W ...

  10. Tomcat编译jsp生成Servlet文件的存放位置

    转自:http://www.cnblogs.com/Leon5/archive/2010/12/07/1899300.html Tomcat将jsp编译成servlet后的文件存放在\work\Cat ...