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

101 篇文章 1 订阅

大体题意:

一开始你有一个空的字符串,你要得到n 个‘a’字符,  你插入或者删除一个‘a’需要x秒,复制目前的字符串需要y秒,求最少多少秒!

思路:

赛后补得,没有时间做了= =!

dp思想,令dp[i]表示第i 个‘a’字符至少需要多少秒!

首先dp[1]肯定是x秒,  一开始只能插入一个字符‘a’!

然后后面的 如果i 是偶数,那么首先这个字符串可以由复制得来!  dp[i] = dp[i/2] + y;

然后在考虑 插入或删除字符!

插入字符肯定是越来越多,因此从前向后遍历,删除字符从后向前遍历!  就是简单的从前一个状态 + x秒!

因为n比较大 为了思路清晰,可以一对一对的遍历,就是先考虑1~2 ,在考虑2~4,在考虑4~8,,,

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = (int)2e7;
ll dp[maxn];
int main(){
//    cout << inf << endl;
    int n,x,y;
    scanf("%d %d %d",&n,&x,&y);
    dp[1] = x;
    int cur = 1;
    while(cur <= n){
        for (int i = cur; i <= 2*cur; ++i){
            if (i == 1)continue;
            dp[i] = inf;
            if(i % 2 == 0) dp[i] = min(dp[i/2] + y,dp[i]);
        }
        for (int i = cur; i <= 2*cur; ++i){
            if (i == 1)continue;
            dp[i] = min(dp[i-1] + x,dp[i]);
        }
        for (int i = 2*cur; i >= cur; --i){
            if (i == 2)continue;
            dp[i-1] = min(dp[i] + x,dp[i-1]);
        }
        cur *= 2;
    }
    printf("%I64d\n",dp[n]);
    return 0;
}

/*
10000000 1000000000 1000000000
382 81437847 324871127

*/

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



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值