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,将当前的字符数量翻倍花费 ...
随机推荐
- node属性
认识node的方法 1.dom.nodeChildrens 用于获取dom下的子元素节点 2.dom.nodeType 用于获取dom节点的属性.共有12种属性,实用属性3种. 元素节点=>1 ...
- MariaDB——数据库基础与sql语句
数据库介绍 什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理数据 ...
- JavaNIO第一话-Buffer
Buffer是入门Java NIO的基础,本文希望通过一些形象的比喻来解释一下缓冲区的概念,帮助读者快速理解和记忆. 本文灵感来自于Bilibili博主v若水若水分享的尚硅谷Java视频_NIO视频教 ...
- [转载]JDK自带的实用工具——native2ascii.exe
做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,原因是编码方式的不一致.native2ascii是sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt, ...
- Spring JMSTemplate 与 JMS 原生API比较
博客分类: JMS Spring 2.x JMSUtil与Spring JmsTemplate的对比 Author:信仰 Date:2012-4-20 未完待续,截止日期2012-4-20 从以下 ...
- 【LOJ2513】「BJOI2018」治疗之雨
题意 你现在有 \(m+1\) 个数:第一个为 \(p\) ,最小值为 \(0\) ,最大值为 \(n\) :剩下 \(m\) 个都是无穷,没有最小值或最大值.你可以进行任意多轮操作,每轮操作如下: ...
- 【转】WdatePicker日历控件使用方法
转 自: https://www.cnblogs.com/yuhanzhong/archive/2011/08/10/2133276.html WdatePicke官 ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- 旧iPhone遭禁,会让苹果产业链迎来新转机吗?
过去几个月,苹果的日子并不好过,先是新iPhone定价过高导致销售疲软,股价连续下跌,万亿市值失守,被微软和亚马逊超越:手机销量上则被华为赶超,整个iPhone产业链都有点儿"哀鸿遍野&qu ...
- 【LeetCode】基本计算器II
[问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 .整数除法仅保留整数部分. 输入: "3+2*2" ...