大体题意:
一开始你有一个空的字符串,你要得到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
memory limit per test
input
output
zscoder
n
x seconds to insert or delete a letter 'a' from the text file and y
zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n
Input
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
t
Examples
input
8 1 1
output
4
input
8 1 10
output
8