[CF710E]Generate a String(DP)

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

题意:生成一个全是a的字符串,长度为n,有三个操作:

1.添加一个a,花费为x。

2.删除一个a,花费为x。

3.复制当前所有a,花费为y。比如:aaaa->aaaaaaaa花费为y。

求达到n的最小花费

思路:dp(i)为i个a时的最小花费,初始化dp(1)=x。

方程:

i为偶数:

dp(i) = min(dp(i-1)+x, dp(i/2)+y)

i为奇数:

dp(i) = min(dp(i-1)+x, dp((i+1)/2)+x+y)

 1 /*
 2 ━━━━━┒ギリギリ♂ eye!
 3 ┓┏┓┏┓┃キリキリ♂ mind!
 4 ┛┗┛┗┛┃\○/
 5 ┓┏┓┏┓┃ /
 6 ┛┗┛┗┛┃ノ)
 7 ┓┏┓┏┓┃
 8 ┛┗┛┗┛┃
 9 ┓┏┓┏┓┃
10 ┛┗┛┗┛┃
11 ┓┏┓┏┓┃
12 ┛┗┛┗┛┃
13 ┓┏┓┏┓┃
14 ┃┃┃┃┃┃
15 ┻┻┻┻┻┻
16 */
17 #include <algorithm>
18 #include <iostream>
19 #include <iomanip>
20 #include <cstring>
21 #include <climits>
22 #include <complex>
23 #include <cassert>
24 #include <cstdio>
25 #include <bitset>
26 #include <vector>
27 #include <deque>
28 #include <queue>
29 #include <stack>
30 #include <ctime>
31 #include <set>
32 #include <map>
33 #include <cmath>
34 using namespace std;
35 #define fr first
36 #define sc second
37 #define cl clear
38 #define BUG puts("here!!!")
39 #define W(a) while(a--)
40 #define pb(a) push_back(a)
41 #define Rint(a) scanf("%d", &a)
42 #define Rs(a) scanf("%s", a)
43 #define Cin(a) cin >> a
44 #define FRead() freopen("in", "r", stdin)
45 #define FWrite() freopen("out", "w", stdout)
46 #define Rep(i, len) for(int i = 0; i < (len); i++)
47 #define For(i, a, len) for(int i = (a); i < (len); i++)
48 #define Cls(a) memset((a), 0, sizeof(a))
49 #define Clr(a, x) memset((a), (x), sizeof(a))
50 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
51 #define lrt rt << 1
52 #define rrt rt << 1 | 1
53 #define pi 3.14159265359
54 #define RT return
55 #define lowbit(x) x & (-x)
56 #define onenum(x) __builtin_popcount(x)
57 typedef long long LL;
58 typedef long double LD;
59 typedef unsigned long long ULL;
60 typedef pair<int, int> pii;
61 typedef pair<string, int> psi;
62 typedef pair<LL, LL> pll;
63 typedef map<string, int> msi;
64 typedef vector<int> vi;
65 typedef vector<LL> vl;
66 typedef vector<vl> vvl;
67 typedef vector<bool> vb;
68 
69 const int maxn = 20000010;
70 int n, x, y;
71 LL dp[maxn];
72 
73 int main() {
74     // FRead();
75     while(~scanf("%d%d%d",&n,&x,&y)) {
76         if(n == 1) {
77             cout << x << endl;
78             continue;
79         }
80         dp[1] = x;
81         For(i, 2, n+1) {
82             dp[i] = dp[i-1] + x;
83             if(i % 2 == 0) dp[i] = min(dp[i], dp[i/2]+y);
84             else dp[i] = min(dp[i], dp[(i+1)/2]+x+y);
85         }
86         cout << dp[n] << endl;
87     }
88     RT 0;
89 }
原文地址:https://www.cnblogs.com/kirai/p/5798237.html