Codeforces 679B - dp

Description

Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.

A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, …, ak has the total volume a13 + a23 + … + ak3.

Limak is going to build a tower. First, he asks you to tell him a positive integer X — the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn’t exceed X.

Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.

Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≤ m that results this number of blocks.
Input

The only line of the input contains one integer m (1 ≤ m ≤ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Output

Print two integers — the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
Sample Input

48
Sample Output

9 42

题意

有一个人在玩堆积木的游戏,给你一个X,这个人会贪心选择一个最大的数,使得这个数a^3<=x,然后堆上去,x-=a^3 然后一直重复这个过程,现在给你一个m,你需要在[1,m]里面找到最大的x,使得使用的数最多,在使用的数最多的情况下,这个数尽量大

题解:

dp,每次你有两种决策:对于当前可用的最大的木块a,你可以使用它,剩余的就是m-a^3;你也可以不使用它,那么当前剩余值等于a^3-1,这时就要选择a-1了,剩余价值就是a^3-1-(a-1)^3.
然后dfs去处理就好了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

pair<ll,ll> best;

ll pow(ll x){return x*x*x;}
void dfs(ll m,ll step,ll X)
{
    if(m==0){
        best=max(best,make_pair(step,X));return;
    }
    ll x=1;
    while(pow(x)<=m)x++;
    x--;
    dfs(m-pow(x),step+1,X+pow(x));
    dfs(pow(x)-1-pow(x-1),step+1,pow(x-1)+X);
}
int main()
{
    ll m;
    cin>>m;
    dfs(m,0,0);
    cout<<best.first<<' '<<best.second;
    return 0;
}

转载于:https://www.cnblogs.com/01world/p/5651200.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值