zoukankan      html  css  js  c++  java
  • Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs

    D. Bear and Tower of Cubes

    题目连接:

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

    Description

    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.

    Sample Input

    48

    Sample Output

    9 42

    Hint

    ## 题意
    有一个人在玩堆积木的游戏,给你一个X,这个人会贪心选择一个最大的数,使得这个数a^3<x,然后堆上去,x-=a^3
    
    然后一直重复这个过程,
    
    现在给你一个m,你需要在[1,m]里面找到最大的x,使得使用的数最多,在使用的数最多的情况下,这个数尽量大
    
    -----------------------------------
    
    ## 题解:
    直接dfs,每次你有两种决策
    
    要么你就使用当前可用的满足x>=p^3
    
    要么你就不使用它,使得当前剩余值等于p^3-1,因为只有这样你才用不上p
    
    然后dfs去处理就好了
    

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    map<long long ,pair<int ,long long> >H;
    long long getmax(long long x)
    {
        if(x==1)return 1;
        long long l=1,r=100005,ans=1;
        while(l<=r)
        {
            long long mid = (l+r)/2LL;
            if(mid*mid*mid<=x)ans=mid,l=mid+1;
            else r=mid-1;
        }
        return ans;
    }
    pair<int,long long> solve(long long x){
        if(x==0)return make_pair(0,0);
        if(H.count(x))return H[x];
        auto &tmp = H[x];
        long long p = getmax(x);
        tmp=solve(x-p*p*p);
        tmp.first++;
        tmp.second+=p*p*p;
        auto tmp2 = solve(p*p*p-1);
        tmp=max(tmp,tmp2);
        return tmp;
    }
    void QAQ()
    {
        long long m;scanf("%lld",&m);
        pair<int,long long>ans=solve(m);
        cout<<ans.first<<" "<<ans.second<<endl;
    }
    int main()
    {
        QAQ();
    }
  • 相关阅读:
    Java 单链表的倒置
    Android查询:模拟键盘鼠标事件(adb shell 实现)
    安卓 发送短信两种方式
    java tcp socket实例
    Java中读取某个目录下的所有文件和文件夹
    Android剖析和运行机制
    linux下搭建android NDK开发环境
    把log存起来
    判断Android系统net和wap接入点的开发实例
    android 4.0.4系统下实现apk的静默安装和启动
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5572139.html
Copyright © 2011-2022 走看看