题目链接:Codeforces 679B Bear and Tower of Cubes
B. 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
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 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,让你找到一个X整数 <= m,要求选择若干个立方体使得体积之和为X 且立方体个数最多,在立方体个数相同的情况下选择较大的X。注:每次选择最大的立方体边长a使得a*a*a <= X。
思路:推敲后发现若ans是一个最优解时,那么当ans + p*p*p >= (p+1)(p+1)(p+1)时,ans + p*p*p一定不是最优解。这样的话只有两种决策,每次选择最大边长a或者选择边长a-1(此时X最优为a^3-1)。
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
int Solve(LL m) {
int l = 1, r = 100000, ans;
while(r >= l) {
int mid = (l + r) >> 1;
if(1LL * mid * mid * mid > m) {
r = mid - 1;
}
else {
ans = mid;
l = mid + 1;
}
}
return ans;
}
LL ans1, ans2;
void DFS(LL m, LL cnt, LL sum) {
if(m <= 7) {
if(ans1 < cnt + m || (ans1 == cnt + m && ans2 < sum + m)) {
ans1 = cnt + m; ans2 = sum + m;
}
return ;
}
int p = Solve(m);
//cout << p << ' ' << m << endl;
DFS(m - 1LL * p * p * p, cnt + 1, sum + 1LL * p * p * p);
DFS(1LL * p * p * p - 1 - 1LL * (p-1) * (p-1) * (p-1), cnt + 1, sum + 1LL * (p-1) * (p-1) * (p-1));
}
int main()
{
LL m;
while(scanf("%lld", &m) != EOF) {
ans1 = ans2 = 0;
DFS(m, 0, 0);
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}