zoukankan      html  css  js  c++  java
  • codeforces 710E E. 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 ≤ 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('
    ');
    }
     
    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;
    }
    

      

  • 相关阅读:
    Ajax原生XHR对象
    node-sass报错解决方法
    AngularJS中的表单验证
    javaScirpt事件详解-原生事件基础(一)
    jQuery Ajax总结
    Ruby 方法
    JS中常遇到的浏览器兼容问题和解决方法
    History对象
    转码与解码
    Location对象
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5801174.html
Copyright © 2011-2022 走看看