Generate a String
Problem Description:
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.
Sample Input:
8 1 1
8 1 10
Sample Output:
4
8
最近好堕落啊,都快一周没有好好刷题了,也不能总是靠队友啊,真的要自己系统的复习下了,还有要省赛了,希望得到好成绩吧 ︿( ̄︶ ̄)︿
【题目链接】Codeforces 710E
【题目类型】dp
&题意:
给你一个空的文本,你的任务是弄出n个字符 'a' ,添加或删除费x个单位时间,复制费y个单位时间,你需要求出最小时间。
&题解:
注意:题意是必须是n个字符,多了是不可以的,要减少到正好是n个才可以。
之后看范围,是1e7,比较明显的dp,既然dp那么我们就要分析他有多少种状态。
首先任何一个dp[i]都可以由dp[i-1]+x推出,之后当i是偶数时,dp[i]还可以=dp[i/2]+y,之后取他们的最小值,但貌似还有什么没用上,那就是减的情况。
什么时候会用到减的情况呢?仔细想一想,他只可以减一个,那么他先加1在减1是没意义的,而且不可能这么做,因为要求最小时间,如果先加1在减1,只会增加时间,所以是没意义的。
那么只剩一种情况了,就是先乘2在减1就好了。一共就这3种情况,都考虑到就可以过了。
【时间复杂度】O(n)
&代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 10000000 + 5 ;
ll s[MAXN];
int n, x, y;
void Solve() {
while (cin >> n >> x >> y) {
s[1] = x;
for (int i = 2; i <= n; i++) {
s[i] = s[i - 1] + x;
if (!(i & 1)) {
s[i] = min(s[i], s[i / 2] + y);
}
else {
s[i] = min(s[i], s[(i + 1) / 2] + x + y);
}
}
cout << s[n] << endl;
}
}
int main() {
Solve();
return 0;
}