贪心算法---codeforce680D Bear and Tower of Cubes

在这款趣味数学挑战中,小北极熊Limak利用无限的立方体块建造高塔,目标是在不超过指定体积限制的前提下,使塔包含尽可能多的立方体块。玩家需选择最优的总体积X,使得塔的块数最大化且X不超过给定的最大值m。通过贪心策略和深度优先搜索(DFS),算法寻找构建最大塔的解决方案。

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

D. Bear and Tower of Cubes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
48
outputCopy
9 42
inputCopy
6
outputCopy
6 6

每一次选择最大的x3和次大的x3计算两个中更优的那个,贪心加dfs。

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int MAXN=1e5+10;
ll x3[MAXN];

pair<ll,ll>ans(0,0);

void dfs(ll m,ll cnt,ll t)
{
    if(!m){
        ans=max(ans,make_pair(cnt,t));
        return ;
    }
    int q=upper_bound(x3+1,x3+100002,m)-x3;
    q--;
    dfs(m-x3[q],cnt+1,t+x3[q]);
    if(q>1)dfs(x3[q]-1-x3[q-1],cnt+1,t+x3[q-1]);
}
int main()
{
    ll m;
    for(ll i=1;i<=100001;i++)
        x3[i]=i*i*i;
    scanf("%I64d",&m);
    dfs(m,0,0);
    printf("%I64d %I64d\n",ans.first,ans.second);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值