【题目】
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.
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.
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.
48
9 42
6
6 6
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 is 42 - 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的塔,用正方形方块尽可能的堆满,要求选取尽可能大的方块堆,其次使堆的方块数目尽可能的多!
【解题思路】贪心加DFS,不太好说,看代码理解一下吧。
【AC代码】
#include <bits/stdc++.h>
using namespace std;
#define ll long long
pair<ll,ll>ans={0,0};//first是方块的数目,second是方块的体积
ll jpow(ll x)
{
return x*x*x;
}
void dfs(ll m,ll num,ll x)
{
if(m==0){
ans=max(ans,make_pair(num,x));
return ;
}
ll cnt=1;
while(jpow(cnt+1)<=m) cnt++;//贪心选最大的m,但不超过m的方块数
dfs(m-jpow(cnt),num+1,x+jpow(cnt));//在此基础上,DFS寻找次大于剩下体积的方块
dfs(jpow(cnt)-jpow(cnt-1)-1,num+1,x+jpow(cnt-1));//找到能填充的方块后,尝试寻找能够代替这个方块的小方块
}
ll n;
int main(){
cin>>n;
dfs(n,0,0);
cout<<ans.first<<" "<<ans.second<<endl;
return 0;
}