codeforces 710E E. Generate a String(dp)
题目链接:
2 seconds
512 megabytes
standard input
standard 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.
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.
Print the only integer t — the minimum amount of time needed to generate the input file.
8 1 1
4
8 1 10
8
题意:
现在添加一个或者删除一个a需要x,复制一次需要y,问正好有n个a至少需要多少时间;
思路:
dp[i]表示正好i个a需要的最少时间,i为偶数的时候dp[i]=min{dp[i-1]+x,dp[i+1]+x,dp[i/2]+y}
i为奇数的时候dp[i]=min{dp[i-1]+x,dp[i+1]+x};
这样写的状态转移方程没法转移啊,把这些式子组合考虑一下可以知道:
当确定了i的dp[i]的时候,可以得到dp[i*2]的估计值为dp[i]+y;这就是得到了偶数的估计值,
所以i%2==1时dp[i]=min{dp[i-1]+x,dp[i+1]+x};i%2==0时dp[i]=min{dp[i-1]+x,dp[i/2]+y};
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e7+10;
const int maxn=1e3+20;
const double eps=1e-12; LL dp[N],x,y;
int n;
int main()
{ read(n);read(x);read(y);
dp[1]=x;dp[2]=x+y;
for(int i=2;i<=n;i++)
{
if(i&1)dp[i]=min(dp[i-1]+x,dp[i+1]+x);
else dp[i]=min(dp[i-1]+x,dp[i/2]+y);
dp[2*i]=dp[i]+y;
}
cout<<dp[n]<<endl;
return 0;
}
codeforces 710E E. Generate a String(dp)的更多相关文章
- Codeforces 710 E. Generate a String (dp)
题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. d ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- codeforces 710E Generate a String(简单dp)
传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...
随机推荐
- Java的集合框架
01.为什么要使用集合框架? 解析:如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象,那么可以使用Java集合框架. 如果启用集合的删除方法,那么集合中所有元素的索引会自动维护. 集合 ...
- 数据库的有关知识==>>我们的血泪史之经典练习(1-2)
今天给大家说说数据库的有关知识 抒情一下,想在好困,真的,虽然我在这温暖的教室,身边有知心的盆友, ,很高兴还能是学生的一员,我们还年轻,我们也不会想的太多,高高兴兴上学,快快乐乐回家,每天吃的饱饱, ...
- Jaxb解析xml准换为javabean
先说下这个的背景吧,前些日子,有个以前的小同事说刚接触webservice,想解析下xml,记得我学的时候还是dom4j,sax的解析方式,最近看别人的代码用的jaxb的方式,觉得注解起来很简练,所以 ...
- IOS缓存机制详解
资料均来自互联网,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 人魔七七:http://www.cnblogs.com/qiqibo/ 为什么要有缓存 应用需要 ...
- 改变mvc web api 支持android ,ios ,ajax等方式跨域调用
公司一个移动后端的项目用到了 webapi 项目搭建到外网环境共app开发者调用测试接口时遇到了一个问题 接口不允许跨域调用 .查阅资料明白 同源策略原则根据请求报头值 Origin 与回应报头值 A ...
- Study Tips
1. 100% width divs not spanning entire width of the browser in webkit. Even Body can not span the en ...
- JavaScript的作用域和闭包
首发于:https://mingjiezhang.github.io/ 闭包和作用域有着千丝万缕的联系. js的作用域 具体的作用域我就不展开叙述了.其中很重要的两点就是:js的作用域链机制和函数词法 ...
- Failed to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 if SQL Server version was lower than 2012
I tried to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 for client. after generated n ...
- Android-BaseLine基础性开发框架
比较基础性的Android快速开发框架Android-BaseLine,Android-BaseLine实现的功能远远没有其他框架多,一个很好的框架不应该显得太过臃肿,很多功能一般情况下我们可能用不到 ...
- eclipse一直卡住,出现 “android sdk content loader 0%” 卡住的错误分析及解决方法
分析:这种问题之前没有遇到过,也不知道什么原因,直接去网上查询,打开www.stackoverflow.com,输入要查询问题的关键词,我们输入 “android sdk content loader ...