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次询问,每次询问给 ...
随机推荐
- http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html
http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html
- 《linux内核设计与实现》读书笔记第十七章
第17章 设备与模块 四种内核成分 设备类型:在所有 Unix 系统中为了统一普通设备的操作所采用的分类. 模块: Linux 内核中用于按需加载和卸载目标码的机制. 内核对象:内核数据结构中支持面向 ...
- C# WebService输出JSON 实现二
一般js请求web服务uk可以通过 contentType: "application/json" 获取json效果,为了取得更好的效果,可以在服务端强制返回JSON格式 服务端 ...
- [原创]Oracle 12c 抢先安装手迹
[前言] Oracle 12c 终于投放市场了,唉,等了很久了.据官方说这是一个为云计算平台量身定做的版本....且不管真的假的,先让我们把它装上再说. 注:笔者在安装的过程中发现12c的安装过程,较 ...
- 在Mac OS X 下快速安装Nginx
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica; color: #8e68ff } p.p2 { margin: 0.0p ...
- Python学习笔记 for windows 三
多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 哺乳类:能跑的哺乳类,能飞的哺乳类: 鸟类:能跑的鸟类,能飞的鸟类. class Animal(object): ...
- Java 静态变量,常量和方法
static 关键字 例如:在球类中使用PI这个常量,可能除了本类需要这个常量之外,在另外一个圆类中也需要使用这个常量.这时没有必要 在两个类中同时创建PI这个常量,因为这样系统会将这两个不在同一个类 ...
- Linux-001-nmon系统性能监控工具的使用及报表产出
在进行性能测试的时候,需要获取服务器的各项指标,例如 CPU.MEM.I/O.DISK 等.网上有很多的监控工具,nmon 就是其中的一个,其可与 JMeter结合使用,测试系统的性能.其概要的介绍, ...
- iOS开发中的权限
权限分类 联网权限 相册权限 相机.麦克风权限 定位权限 推送权限 通讯录权限 日历.备忘录权限 联网权限 引入头文件 @import CoreTelephony; 应用启动后,检测应用中是否有联网权 ...
- Python开发程序:学员管理系统(mysql)
主题:学员管理系统 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节 ...