题目链接:http://www.codeforces.com/problemset/problem/710/E
E. Generate a String
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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.
解析:定义数组dp,dp[i]的含义为:生成i个字符所需要的最小的时间。对于任意dp[i],有以下几种方法可以得到它:1. dp[i] = d[i/2] + y(直接对i/2个字符进行复制得到i个);2. dp[i] = dp[i-1] + x(对i-1个字符手动输入一个得到i个);3. dp[i] = dp[i+1] + x(对i+1个字符手动删除一个得到i个)。
代码如下:
#include <iostream>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
//cout << "OK" << endl;
#define _clr(x,y) memset(x,y,sizeof(x))
#define _inf(x) memset(x,0x3f,sizeof(x))
#define pb push_back
#define mp make_pair
#define FORD(i,a,b) for (int i=(a); i<=(b); i++)
#define FORP(i,a,b) for (int i=(a); i>=(b); i--)
#define REP(i,n) for (int i=0; i<(n); i++)
using namespace std;
//const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
LL pow_mod(LL a,LL n,LL m)
{
if(n == 0) return 1;
LL x = pow_mod(a,n>>1,m);
LL ans = x*x%m;
if(n&1) ans = ans*a%m;
return ans;
}
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}
const LL inf = 0x3f3f3f3f3f3f3f;
LL dp[20000000+10];
int main(){
int n, x, y;
cin >> n >> x >> y;
int upbound = 1;
dp[1] = x;
while(upbound < n){
FORD(i,upbound+1,upbound*2){
if(i&1) dp[i] = inf;
else dp[i] = dp[i/2] + y;
}
FORD(i,upbound,upbound*2-1){
dp[i+1] = min(dp[i+1],dp[i]+x);
}
FORP(i,upbound*2,upbound+1){
dp[i-1] = min(dp[i-1],dp[i]+x);
}
upbound *= 2;
}
cout << dp[n] << endl;
return 0;
}