zoukankan      html  css  js  c++  java
  • CodeForce 710E

    Generate a String
     

      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个‘a’),x(增加或删除一个‘a’需要多少秒),y(复制并粘贴当前的字符串需要多少秒)
    求用最少的时间,生成n个‘a’;
    思路:
      dp题目,当n == 0, 1 时需要时间为0, x
      n为奇数时:可以是 n - 1 加上个‘a’ 或者 n + 1 删除一个‘a’
      n为偶数时:就得看看 复制一半个‘a’需要的时间长,还是一个一个的加的时间长
      
    AC代码:
     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 ll a, b, n;
     5 ll dp(ll i)
     6 {
     7     if(i == 0) 
     8         return 0;
     9     if(i == 1) 
    10         return a;
    11     if(i % 2)
    12     {
    13         ll i1 = dp(i - 1), i2 = dp(i + 1);
    14         return a + min(i1, i2);
    15     }
    16     else if(i / 2 * a <= b)
    17         return i * a;
    18     else 
    19         return b + dp(i / 2);
    20 }
    21 int main()
    22 {
    23     while(~scanf("%I64d", &n))
    24     {
    25         scanf("%I64d%I64d", &a, &b);
    26         printf("%I64d
    ", dp(n));
    27     }
    28     return 0;
    29 }
    View Code
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    戒烟与苦乐原则
    计算机视觉(二)-opencv之createTrackbar()详解
    计算机视觉(一)-openCV的安装及使用
    友谊之光
    深度学习-神经网络
    理解与学习深度卷积生成对抗网络
    修改路由器用的校园网账号
    参加Folding@Home(FAH)项目,为战胜新冠肺炎贡献出自己的一份力量
    更改路由器为老版本固件的教程
    逻辑回归(Logistic Regression)详解,公式推导及代码实现
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5798256.html
Copyright © 2011-2022 走看看