codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)

题目链接:

D. Bear and Tower of Cubes

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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

题意:

给一个上限,现在要你求一个值,这个值是用最多的小正方体拼接起来的,而且体积和尽量大;

思路:

每次贪心的选取,每次选比它小的或者改变上限选下一个;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+20;
const double eps=1e-12;

LL d[N];
pair<int,LL>ans;
int search(LL x)
{
    int l=0,r=N-1;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(d[mid]<=x)l=mid+1;
        else r=mid-1;
    }
    return l-1;
}
int dfs(LL cur,LL temp,int num)
{
    if(num>ans.first)ans.first=num,ans.second=temp;
    else if(num==ans.first&&temp>ans.second)ans.second=temp;

    int x=search(cur);
    if(x==0)return 0;
    dfs(cur-d[x],temp+d[x],num+1);
    dfs(d[x]-1-d[x-1],temp+d[x-1],num+1);
}

int main()
{
    LL m;
    ans.first=0;ans.second=0;
    read(m);
    for(int i=1;i<N;i++)d[i]=(LL)i*i*i;
    dfs(m,0,0);
    cout<<ans.first<<" "<<ans.second<<endl;
    return 0;
}

  

posted @ 2016-08-20 15:00  LittlePointer  阅读(339)  评论(0编辑  收藏  举报