cf 710 E Generate a String
题意:
开始你有数字$0$,你可以用代价$x$将该数字加$1$或减$1$(当$x > 0$时),或用代价$y$将该数字变为$2x$,那么问得到数字$n$所需的最少代价是多少。
数据范围$1 \leq x, y \leq 10^9$,$1 \leq n \leq 10^7$。
分析:
记得到数字$n$的代价为$f(n)$。
不加证明地给出如下结论:
注:可以通过观察数的二进制表达形式归纳证明下面的结论。
若$x = y$,则
$1^{\circ}$若$n$为偶数,则$f(n) = f(\frac{n}{2}) + y$ $(*)$
$2^{\circ}$若$n$为奇数,则$f(n) = min(f(n - 1), f(n + 1)) + x$ $(\#)$
把$(*)$当作主式,用$(\#)$式作为递推中间项,很容易计算出所有偶数位置对应的$f$值,从而计算出所有$f$值。
当$x, y$大小关系不确定时,我们只需修改偶数情况的更新规则。当$n$为偶数时,为了计算$f(n)$,需要归约到$f(1) = x$,在此过程中如果用到加倍操作,那么在当前位置
用效果必然最好(直观上如此,实际也可证明),否则就不用加倍操作。使用加倍操作后转移到$f(n / 2)$,代价为$y$,而不用加倍操作转移到$f(n / 2)$时代价为$x \cdot \frac{n}{2}$,使用加倍操作当且仅当$y <= x \cdot \frac{n}{2}$,因此将$(*)$更新为:
若$y <= x \cdot \frac{n}{2}$,$f(n) = f(\frac{n}{2}) + y$
否则$f(n) = n \cdot x$
可以用记忆话搜索来处理答案,空间复杂度$O(n)$,时间复杂度$O(log(n))$。
此外,还可以通过使用队列首先更新花费代价较小的位置来寻找答案,时间复杂度是$O(n)$,类似于$\text{bfs}$的过程。
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <functional>
#include <cmath>
#include <iostream>
#include <assert.h>
#pragma comment(linker, "/STACK:102400000,102400000")
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mp std :: make_pair
#define st first
#define nd second
#define keyn (root->ch[1]->ch[0])
#define lson (u << 1)
#define rson (u << 1 | 1)
#define pii std :: pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define type(x) __typeof(x.begin())
#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define ROF(i, t, s) for(int i = (t); i >= (s); i--)
#define dbg(x) std::cout << x << std::endl
#define dbg2(x, y) std::cout << x << " " << y << std::endl
#define clr(x, i) memset(x, (i), sizeof(x))
#define maximize(x, y) x = max((x), (y))
#define minimize(x, y) x = min((x), (y))
using namespace std;
typedef long long ll;
const int int_inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const int INT_INF = (int)((1ll << ) - );
const double double_inf = 1e30;
const double eps = 1e-;
typedef unsigned long long ul;
typedef unsigned int ui;
inline int readint() {
int x;
scanf("%d", &x);
return x;
}
inline int readstr(char *s) {
scanf("%s", s);
return strlen(s);
} class cmpt {
public:
bool operator () (const int &x, const int &y) const {
return x > y;
}
}; int Rand(int x, int o) {
//if o set, return [1, x], else return [0, x - 1]
if (!x) return ;
int tem = (int)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
}
ll ll_rand(ll x, int o) {
if (!x) return ;
ll tem = (ll)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
} void data_gen() {
srand(time());
freopen("in.txt", "w", stdout);
int kases = ;
//printf("%d\n", kases);
while (kases--) {
ll sz = ;
printf("%d\n", sz);
FOR(i, , sz) {
int o = Rand(, );
int O = Rand(, );
putchar(O + (o ? 'a' : 'A'));
}
putchar('\n');
}
} const int maxn = 1e7 + ;
ll n, x, y;
ll dp[maxn]; ll cal(ll num) {
if (dp[num] != -) return dp[num];
if (num == ) return dp[num] = x;
if (num & ) return dp[num] = x + min(cal(num - ), cal(num + ));
if ((num >> ) * x >= y) return dp[num] = y + cal(num >> );
else return dp[num] = num * x;
} int main() {
//data_gen(); return 0;
//C(); return 0;
int debug = ;
if (debug) freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while (~scanf("%lld%lld%lld", &n, &x, &y)) {
clr(dp, -);
ll ans = cal(n);
printf("%lld\n", ans);
}
return ;
} //382 81437847 324871127
cf 710 E Generate a String的更多相关文章
- 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
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- 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 ...
- 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 E. Generate a String(dp)
题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- 22.Generate Parentheses (String; Back-Track)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- CF 1117 E. Decypher the String
E. Decypher the String 链接 题意: 有一个字符串,一些操作,每次操作交换两个位置的字符,经过这些操作后,会得到新的字符串.给你新的字符串,求原来的串.可以有3次询问,每次询问给 ...
随机推荐
- java中类继承,到底继承了什么?
继承的最大好处就是为了实现代码的复用.那么,子类到底从父类得到的什么呢? 实例成员 父类的private成员不会被子类继承,子类不能访问.但是子类对象的确包含父类的私有成员. 父类的 包访问成员 继承 ...
- 解决国内npm依赖包安装慢的问题
可以通过 npm config set registry http://registry.npm.taobao.org命令设置镜像服务器为阿里巴巴镜像服务器,速度会快很多,亲测有效.
- Stripe开发笔记之-ISO Currency Code(ISO 4217)
参考:维基百科和ISO官网 ISO Currency Code:国际标准组织通用货币代号,国际标准化组织(英语:International Organization for Standardizati ...
- [原创]快速指定SQLDeveloper所使用JDK的方法
就众多的免费SQL开发工具来讲,Oracle出品的SQLDeveloper 还是一个很不错的选择. 看到网上不少的帖子讨论SQL Developer 启动的时候报告找不到Java Home(或JDK) ...
- 【leedcode】longest-substring-without-repeating-characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- Web测试的常用测试用例与知识
1. Web测试中关于登录的测试 2. 搜索功能测试用例设计 3. 翻页功能测试用例 4. 输入框的测试 5. Web测试的常用的检查点 6. 用户及权限管理功能常规测试方法 7. Web测试之兼容性 ...
- Python开发【前端】:汇总
页面模板 1.EasyUI(推荐指数★) JQuery EasyUI中文网 下载 使用方法:把文件下载到本地.直接从官网上把源码拷贝过来,更改下js的路径即可 优点:功能非常多.非常齐全 偏做后台管理 ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增消息管理
在V3.0版本的Web(Mvc.WebForm)与WinForm中我们新增了“消息管理”模块.“消息管理”模块是对框架的所有消息进行管理.通过左侧的消息分类可以查看所选分类的所有消息列表.在主界面上我 ...
- ubuntu安装使用latex和texmaker--PC端
参考文档 据说中文文献可能不识别,可能用到的参考资料
- HandlerMapping的3种访问形式
1.根据BeanName访问Controller 2.根据简单URL访问 3.根据Controller类名访问