codechef - Bytelandian gold coins 题解
来源:互联网 发布:风险矩阵法优缺点 编辑:程序博客网 时间:2024/06/10 00:38
In Byteland they have a very strange monetary system.
Each Bytelandian gold coin has an integer number written on it. A coin n
can be exchanged in a bank into three coins: n/2, n/3 and n/4.
But these numbers are all rounded down (the banks have to make a profit).
You can also sell Bytelandian coins for American dollars. The exchange
rate is 1:1. But you can not buy Bytelandian coins.
You have one gold coin. What is the maximum amount of American dollars
you can get for it?
Input
The input will contain several test cases (not more than 10). Each
testcase is a single line with a number n, 0 <= n <= 1 000 000 000.
It is the number written on your coin.
Output
For each test case output a single line, containing the maximum amount
of American dollars you can make.
Example
Input:122Output:132
本题使用动态规划法, 或者记忆法,加上递归法。
不加上递归法,好像很麻烦,因为我们不知道其初始值,只知道其最终值,所以只能往下递推了,这样使用记忆法就比动态规划要方便了。
使用二维表设计其递归记忆表,防止重复计算。还是十分困难的,动态规划法有时候不一定比记忆法要好。
递归记忆法的学名: top-down with memoization; Introduction to Algorithm的Dynamic programming 这章有介绍
#include <stdio.h>#include <vector>#include <cmath>using namespace std;class Bytelandiangoldcoins{ long long calMax(vector<vector<long long> > &tbl, int n, int r, int c){if (n < 12) return n;if (tbl[r][c] == 0){tbl[r][c] = calMax(tbl, n>>1, r+1, c) + calMax(tbl, n>>2, r+2, c)+ calMax(tbl, n/3, r, c+1);}return tbl[r][c];}public:Bytelandiangoldcoins(){int r = (int)ceil(log(1E9)/log(2.0));int c = (int)ceil(log(1E9)/log(3.0));int n;while (scanf("%d", &n) != EOF){vector<vector<long long> > tbl(r, vector<long long>(c, 0));printf("%lld\n", calMax(tbl, n, 0, 0));}}};
- codechef - Bytelandian gold coins 题解
- (codechef) Bytelandian gold coins
- Bytelandian gold coins
- 【SPOJ-COINS】Bytelandian gold coins【DP】
- codechef COINS
- Gold Coins
- Gold Coins
- Gold Coins
- Gold Coins
- Gold Coins
- Gold Coins
- Gold Coins
- Gold Coins
- fjnu 1104 Gold Coins
- zoj 2345 Gold Coins
- POJ 2000 Gold Coins
- poj2000 Gold Coins!
- zoj 2345 Gold Coins
- Firebug 网络监视器使用教程英文
- 版本控制工具 GIT入门教程
- jQuery整理笔记五----jQuery事件
- Android IPC通信之Binder通信
- c#设置button、label等控件的背景色为透明
- codechef - Bytelandian gold coins 题解
- using namespace std
- printf("XXX" "YYYY"... "ZZZZ", ...) is OK
- jQuery整理笔记六----jQuery动画
- tableview 编辑整理
- 浏览器的标准模式与怪异模式的设置与区分方法
- jQuery整理笔记七----几个经典表单应用
- 学习笔记1
- git diff