Educational Codeforces Round 16 E. Generate a String
2016-08-25 20:44
344 查看
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.
Examples
input
output
input
output
这题的大意是一个人要输出n个‘a’,他打出或删除一个‘a’需要x秒钟,复制粘贴要y秒钟,问你要输入n个字符‘a’最少要多长时间。
这题其实是一个不难的DP,分奇偶进行DP就行,一个转态只和前面的某两个状态有关。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long dp[10000010];
int main(void)
{
int n,i,j,x,y;
while(scanf("%d%d%d",&n,&x,&y)==3)
{
dp[0] = 0;
for(i=1;i<=n;i++)
{
if(i % 2 == 1)
dp[i] = min(dp[i-1]+x,dp[(i+1)/2]+x+y);
else
dp[i] = min(dp[i-1]+x,dp[i/2]+y);
}
printf("%I64d\n",dp
);
}
}
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.
Examples
input
8 1 1
output
4
input
8 1 10
output
8
这题的大意是一个人要输出n个‘a’,他打出或删除一个‘a’需要x秒钟,复制粘贴要y秒钟,问你要输入n个字符‘a’最少要多长时间。
这题其实是一个不难的DP,分奇偶进行DP就行,一个转态只和前面的某两个状态有关。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long dp[10000010];
int main(void)
{
int n,i,j,x,y;
while(scanf("%d%d%d",&n,&x,&y)==3)
{
dp[0] = 0;
for(i=1;i<=n;i++)
{
if(i % 2 == 1)
dp[i] = min(dp[i-1]+x,dp[(i+1)/2]+x+y);
else
dp[i] = min(dp[i-1]+x,dp[i/2]+y);
}
printf("%I64d\n",dp
);
}
}
相关文章推荐
- day10多线程及创建线程两种方法
- httpServletRequest对象获取请求消息头
- execute与executeQuery和executeUpdate区别
- BLE-NRF51822教程3-sdk程序框架剖析
- 十条不错的编程观点【转载】
- excel单元格间接引用和字符串连接
- 《Matlab算法》 part1 误差分析
- 测试
- 关于H264的补充
- nagios部署监控多台client
- 两个变量交换的四种方法(Java)
- hdu 1372&&uva 439 KnightMoves(bfs)
- leetcode 387 First Unique Character in a String C++
- (四十四)、网络基础与TCP,UDP协议
- dct变换
- quality for code
- 个人主机配置为虚拟服务器
- 百度地图配置使用笔记(AndroidStudio)
- JavaScript学习笔记--语法
- HashMap封装查询到的数据