传送门:http://codeforces.com/contest/680/problem/D
题意:求一个不超过 m 的最大体积 X , 每次选一个最大的 x 使得 x3 不超过当前剩余体积。问在能选最多个数的情况下, X 最大是多少
思路:
对于每一次选择,首先要保证选完后的剩余体积最大,这样就保证了能选最多个数。然后在这基础上保证
X
最大。
考虑对于最大的
a
,使得
a3<=m
.
如果当前选择的是
a
,则剩余体积就是
m1=m−a3
如果当前选择的是
a−1
, 则剩余体积就是
m2=(a3−1)−(a−1)3
. 要保证
a−1
是可选的最大的,对
m
的上限有要求
如果当前选择的是
a−2
, 则剩余体积就是
m3=(a−1)3−1−(a−2)3
.
两个相邻数的立方差是不断递增的,所以在
a>=1
的情况下,
m2
恒不小于
m3
, 所以就不用考虑
a−2
了
这样对于每一个状态,只要考虑
a
和
a−1
. 时间复杂度是
O(m13)
代码:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll __int64
template<class T> T sqr(T x){ return x * x * x; }
ll m;
pair<int, ll> P;
void dfs(ll x, int num, ll sum){
if(!x){ P=max(P, {num, sum}); return ;}//所选的X能到0时更新一下答案
ll t=1;
while(sqr(t+1)<=x)t++;
dfs(x-sqr(t), num+1, sum+sqr(t));
if(x>0)dfs(sqr(t)-1-sqr(t-1), num+1, sum+sqr(t-1));
}
int main(){
scanf("%I64d", &m), dfs(m, 0, 0);
printf("%d %I64d\n", P.ff, P.ss);
return 0;
}
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 volumea13 + 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 Xbetween 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 Xsecondarily 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.