codeforces 679B Bear and Tower of Cubes(贪心+dfs)

本文介绍了一道关于构建立方体塔的算法题目。任务是利用不同尺寸的立方体块构建一个总体积为m的塔,并且尽可能多地使用块。文章详细解释了贪心策略的应用,以及如何通过递归搜索确定最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考http://blog.youkuaiyun.com/sinat_34263473/article/details/51620773
http://www.cnblogs.com/macinchang/p/5572070.html

Bear and Tower of Cubes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
Examples
input

48

output

9 42

input

6

output

6 6

Note

In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.

In more detail, after choosing X = 42 the process of building a tower is:

Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is42 - 27 = 15.
The second added block has side 2, so the remaining volume is 15 - 8 = 7.
Finally, Limak adds 7 blocks with side 1, one by one.

So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7·13 = 27 + 8 + 7 = 42.

题意:要你将一个体积为m的塔,用正方形方块尽可能的堆满,要求选取尽可能大的方块堆,其次使堆的方块数目尽可能的多!

思路:

对于每一次选择,首先要保证选完后的剩余体积最大,这样就保证了能选最多个数。然后在这基础上保证   最大。
考虑对于最大的  ,使得    .
如果当前选择的是  ,则剩余体积就是    
如果当前选择的是  , 则剩余体积就是      . 要保证   是可选的最大的,对   的上限有要求
如果当前选择的是  , 则剩余体积就是      .
所以可以发现在   的情况下,   恒不小于  , 所以就不用考虑   了
这样对于每一个状态,只要考虑   和  . 时间复杂度是       

别人有注释的代码:


LL jpow(LL x)    //立方根的计算
{
    return x*x*x;
}
pair<LL,LL> ans;    //first是方块的数目,second 是方块堆的总体积
void dfs(LL m,LL num, LL x)
{
    if(m==0){              //所堆的体积不能超过m
        ans = max(ans,make_pair(num,x));
        return ;
    }
    LL tpx = 1;
    while(jpow(tpx+1)<=m)  //贪心,选取最大的m,但不超过m个正方形方块
        tpx++;
    dfs(m - jpow(tpx),num+1,x+jpow(tpx));  //在此基础上,DFS寻找次大于剩下体积的方块
    if(tpx - 1 >= 0)                //找到能填充的方块后,尝试寻找能够代替这个方块的小方块
        dfs(jpow(tpx)-1-jpow(tpx - 1),num+1,x+jpow(tpx-1));
}
int main()
{
    LL m;
    scanf("%I64d",&m);
    dfs(m,0,0);
    printf("%I64d %I64d\n",ans.first,ans.second);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值