CodeForces - 710E Generate a String (dp)
题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间。
分析:dp[i]---构造长度为i的串需要花费的最短时间。
1、构造长度为1的串,只能插入,dp[1] = x。
2、当前串的长度i为偶数,可以
(1)长度为i/2的串加倍:dp[i / 2] + y
(2)长度为i-1的串插入一个a:dp[i - 1] + x
3、当前串的长度i为奇数,可以
(1)长度为i/2的串加倍,再加上一个a:dp[i / 2] + y + x
(2)长度为i/2+1的串加倍,再删除一个a:dp[i / 2 + 1] + y + x
(3)长度为i-1的串插入一个a:dp[i - 1] + x
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<cctype>
- #include<cmath>
- #include<iostream>
- #include<sstream>
- #include<iterator>
- #include<algorithm>
- #include<string>
- #include<vector>
- #include<set>
- #include<map>
- #include<stack>
- #include<deque>
- #include<queue>
- #include<list>
- #define lowbit(x) (x & (-x))
- const double eps = 1e-8;
- inline int dcmp(double a, double b){
- if(fabs(a - b) < eps) return 0;
- return a > b ? 1 : -1;
- }
- typedef long long LL;
- typedef unsigned long long ULL;
- const int INT_INF = 0x3f3f3f3f;
- const int INT_M_INF = 0x7f7f7f7f;
- const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
- const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
- const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
- const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
- const int MOD = 1e9 + 7;
- const double pi = acos(-1.0);
- const int MAXN = 1e7 + 10;
- const int MAXT = 10000 + 10;
- using namespace std;
- LL dp[MAXN];
- int main(){
- LL n, x, y;
- scanf("%lld%lld%lld", &n, &x, &y);
- memset(dp, LL_INF, sizeof dp);
- dp[1] = x;
- for(LL i = 2; i <= n; ++i){
- if(i % 2 == 0){
- dp[i] = min(dp[i / 2] + y, dp[i - 1] + x);
- }
- else{
- dp[i] = min(dp[i / 2] + x + y, dp[i - 1] + x);
- dp[i] = min(dp[i], dp[i / 2 + 1] + y + x);
- }
- }
- printf("%lld\n", dp[n]);
- return 0;
- }
CodeForces - 710E Generate a String (dp)的更多相关文章
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- codeforces 710E Generate a String(简单dp)
传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- hdu 4055 Number String(dp)
Problem Description The signature of a permutation is a string that is computed as follows: for each ...
- hdu5707-Combine String(DP)
Problem Description Given three strings a, b and c , your mission is to check whether c is the combi ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- Codeforces 536D - Tavas in Kansas(dp)
Codeforces 题目传送门 & 洛谷题目传送门 其实这题本该 2019 年 12 月就 AC 的(详情请见 ycx 发此题题解的时间),然鹅鸽到了现在-- 首先以 \(s,t\) 分别为 ...
- HDU4055 - number string(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数. 当a[i]=='I'时,dp[i ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
随机推荐
- JS监听video视频播放时间
采用原生时间监听element.addEventListener(event, function, useCapture) //监听播放时间 var video = document.getEleme ...
- 在linux环境中如何删除文件
使用rm -rf 目录名字 命令即可 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 eg 删除文件夹实例:rm -rf /var/log/httpd/acc ...
- python列表元组 魔法方法
1.元祖 count() 统计某个字符串的出现次数 tuple.count('22') 返回一个整数 index() 获取某个值出现的位置 2.列表 字符串可以直接转换列表 l ...
- 17 MySQL的小技巧
1.正则表达式的使用 ^ 在字符串开始处进行匹配 $ 在字符串末尾处进行匹配 . 匹配任意单个字符,包括换行符 [...] 匹配出括号内的任意字符 [^...] 匹配不出括号内的任意字符 a* ...
- layerui上传文件
参考: http://www.layui.com/doc/modules/upload.html <1> 文件上传(以下函数必须要在js文件加载时执行) upload.render({ e ...
- HihoCoder第三周与POJ2406:KMP算法总结
HihoCoder第三周: 输入 第一行一个整数N,表示测试数据组数. 接下来的N*2行,每两行表示一个测试数据.在每一个测试数据中,第一行为模式串,由不超过10^4个大写字母组成,第二行为原串,由不 ...
- pytorch max和clamp
torch.max() torch.max(a):数组a的最大值 torch.max(a, dim=1):多维数组沿维度1方向上的最大值,若a为二维数组,则为每行的最大值(此时是对每行的每列值比较取最 ...
- 冰蝎动态二进制加密WebShell特征分析
概述 冰蝎一款新型加密网站管理客户端,在实际的渗透测试过程中有非常不错的效果,能绕过目前市场上的大部分WAF.探针设备.本文将通过在虚拟环境中使用冰蝎,通过wireshark抓取冰蝎通信流量,结合平时 ...
- java虚拟机开篇01
一直以来对java 基础设施都啥都不知道啊,感觉有时候挺费力,挺吃劲的. 一下是一些很好的参考资料: http://blog.csdn.net/bingduanlbd/article/details/ ...
- 怎样设置使IntelliJ IDEA智能提示忽略大小写?
打开设置(CTRL+ALT+S)打开editor,找到“Code Completion”->点击Match case前面的框不勾选即可.如下图: