FOJ/FZU/FZOJ 1550Monetary System【记忆化搜索】

Problem 1500 Monetary System

Accept: 415    Submit: 1261
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

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 <= 1000000000. 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.

Sample Input

12
2

Sample Output

13
2

Source

FOJ月赛-2007年5月 

题目大意:给你一个特殊的硬币,这个硬币可以换三种硬币:n/2,n/3,n/4,问怎么换能够得到最大硬币价值,翻译的可能有些浅显,我们就样例1来说:
n=12 当n只换一次为: 6 4 3的时候就比价值12大1为13,当然6也可以继续向下兑换,但是因为我们是举例,就不向下分了。

分析:搜索。因为N比较大的时候,暴搜一定会超时,这个时候我们就要想优化的方法。


我们知道,如果输入的n比较大的时候,在深搜的过程中记录一些可能会重复的值就可以省略掉很多很多不需要的操作,因为数组开的大小有限,所以我们只对10^6以下的数据进行记忆化,其他数据我们还是暴力处理。


爆搜输入999999999的时候跑的时间就要取决于你的电脑cpu强大与否了..................... 但是经过处理一些重复数据之后,是用很快的速度就能跑出来的。


需要注意的点:数据要开long long int,千万记住FZU要使用I64d,不能用lld。。。。。。。在wa了一次之后改数据为long long int的时候竟然TLE...........后来才知道要交I64d......好坑爹................【论我们学校oj和别的学校oj后台处理方式不同的坑爹性....................】

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll output;
ll dp[1000000];//只处理10^6以下的数据记忆化,其实数据越小,越需要记忆化,反而值越大,越不需要记忆化,10^6足够了。
ll dfs(ll now)
{
    if(now==0)return 0;
    if(now<=1000000)
    {
        if(dp[now]!=0)return dp[now];
        else
        {
            dp[now]=max(now,dfs(now/2)+dfs(now/3)+dfs(now/4));
            return dp[now];
        }
    }
    else
    {
        return max(now,dfs(now/2)+dfs(now/3)+dfs(now/4));
    }
}
int main()
{
    ll n;
    while(~scanf("%I64d",&n))
    {
        memset(dp,0,sizeof(dp));
        output=dfs(n);
        printf("%I64d\n",output);
    }
}













  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值