Codeforces 710 E. Generate a String (dp)

题目链接:http://codeforces.com/problemset/problem/710/E

加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少。

dp[i]表示i个字符的最小代价。

当i为偶数个的时候,由+1或者*2得到。

当i为奇数个的时候,由+1或者*2-1得到。

 1 //#pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef __int64 LL;
15 typedef pair <int, int> P;
16 const int N = 1e7 + 5;
17 LL dp[N];
18 
19 int main()
20 {
21     int n;
22     LL x, y;
23     cin >> n >> x >> y;
24     dp[1] = x;
25     for(int i = 2; i <= n; ++i) {
26         if(i&1) {
27             dp[i] = min(dp[i - 1] + x, dp[i / 2 + 1] + x + y);
28         } else {
29             dp[i] = min(dp[i / 2] + y, dp[i - 1] + x);
30         }
31     }
32     cout << dp[n] << endl;
33     return 0;
34 }

 

转载于:https://www.cnblogs.com/Recoder/p/5837939.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值