dfs Codeforces Round #356 (Div. 2) D

http://codeforces.com/contest/680/problem/D

题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每次放入的立方体的体积要尽可能大,问最多能放几块?

 

感觉自己还是太菜了。。。这种题目都做不来TAT

 

思路:因为每次都要放入,我们找一下情况以后可以发现,假设当前的体积为x,如果要让cnt个数最多,要么就是要减去x-a*a*a,要么就是让x直接等于a*a*a-1,因此我们很容易就可以得到这是一个dfs的条件,然后我们只要利用dfs就好了

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 100000 + 5;
LL m, cnt;
LL dp[maxn];
map<LL, pair<LL, LL> > mp;///val, cnt and val

pair<LL, LL> dfs(LL m){
    if (m == 0) return mk(0, 0);
    if (mp.count(m)) return mp[m];
    pair<LL, LL> &tmp1 = mp[m];
    int pos = lower_bound(dp + 1, dp + 1 + cnt, m) - dp;
    if (dp[pos] != m) pos--;
    tmp1 = dfs(m - dp[pos]);
    tmp1.fi += 1, tmp1.se += dp[pos];

    pair<LL, LL> tmp2 = dfs(dp[pos] - 1);
    return tmp1 = max(tmp1, tmp2);
}

int main(){
    scanf("%lld", &m);
    for (LL i = 1; 1LL*i*i*i <= m; i++) dp[i] = 1LL * i * i * i, cnt = i;
    cnt++;
    dp[cnt] = 1LL * cnt * cnt * cnt;
    pair<LL, LL> res = dfs(m);
    printf("%lld %lld\n", res.fi, res.se);
    return 0;
}
View Code

 

posted @ 2016-10-01 10:49  知る奇迹に  阅读(428)  评论(0编辑  收藏  举报