题意:Limak要垒一座由立方体垒成的塔。现有无穷多个不同棱长(a>=1)的立方体。要求:1、塔的体积为X(X<=m).2、在小于X的前提下,每次都选体积最大的砖块。3、在砖块数最多的前提下,使X尽可能大。求最终垒成塔所用的最大砖块数和塔可能的最大体积(在砖块数最多的前提下)。

思路:首先找到一个体积最大的砖块first_block( a3 ≤ m)。现在first_block的a有两种选择,a和a-1。

设此时剩余可使用体积为mm。

1、若first_block的棱长为a,则mm= m - a3.(第39行)(将此情况递归,最终将可能的num和current的最大值记录在best中)

2、若first_block的棱长为a-1,则mm= a3 - 1 - (a - 1)3(第41行)

PS:

1、first_block的棱长不选a-2,甚至更小棱长的原因:因为立方体的体积:1,8,27,64,125……显然m-(a-1)^3>(a-2)^3,

且a越大,这种情况越明显。所以选完一个(a-1)为棱长的立方体后,必有剩余体积可再选a-2的,所以只需选a-1的。

2、第33行比较顺序的原因:题意中首先要求砖块数最大,其次X尽可能大。(current累加到最终等于X)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<set>
#include<cctype>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<list>
const int INF = 0x7f7f7f7f;
const double PI=acos(1.0);
typedef long long ll;
typedef unsigned long long llu;
const int MAXN= + ;
using namespace std;
ll p(ll x)//求边长为x的立方体的体积
{
return x*x*x;
}
pair<ll, ll> best;
void rec(ll m, ll num, ll current)//num砖块总数,current目前塔的总体积
{
if(m==)
{
best=max(best, make_pair(num, current));//make_pair()可生成一个pair对象,比较时先比较第一个值,若第一个值相同,再比较第二个值
return;
}
ll x = ;
while(p(x+)<=m)//得到小于等于m的最大体积的立方体的边长
++x;
rec(m-p(x), num+, current+p(x));
if(x - >= )
rec(p(x)--p(x-), num+, current+p(x-));
}
int main()
{
ll m;
scanf("%lld",&m);
rec(m,,);
printf("%lld %lld\n",best.first,best.second);
return ;
}
 

CodeForces 679B(Bear and Tower of Cubes)的更多相关文章

  1. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  2. Codeforces 680D - Bear and Tower of Cubes

    680D - Bear and Tower of Cubes 思路:dfs+贪心,设剩余的体积为res,存在a,使得a3 ≤ res,每次取边长为a的立方体或者边长为a-1的立方体(这时体积上限变成a ...

  3. 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 i ...

  4. 【CodeForces】679 B. Bear and Tower of Cubes

    [题目]B. Bear and Tower of Cubes [题意]有若干积木体积为1^3,2^3,...k^3,对于一个总体积X要求每次贪心地取<=X的最大积木拼上去(每个只能取一次)最后总 ...

  5. 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 i ...

  6. 【19.05%】【codeforces 680D】Bear and Tower of Cubes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. Bear and Tower of Cubes Codeforces - 680D

    https://codeforces.com/contest/680/problem/D 一道2D,又是搞两个小时才搞出来...不过好在搞出来了 官方题解:可以证明对于m,设a是满足a^3<=m ...

  8. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  9. uva 10051 Tower of Cubes(DAG最长路)

    题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...

随机推荐

  1. Codeforces Gym 100286G Giant Screen 水题

    Problem G.Giant ScreenTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/con ...

  2. C#窗体间通讯的几种处理方法

    应用程序开发中,经常需要多窗体之间进行数据通信,写几个例子,把几种常用的通信方式总结一下: 主窗体Form1是一个ListBox,单击选中某列时,弹出窗体Form2,Form2中两个控件,一个是Tex ...

  3. osg 笔记一 (转)

    场景图形采用一种自顶向下的,分层的树状数据结构来组织空间数据集,以提高渲染的效率 场景图形树结构的顶部是一个根节点,从根节点向下延伸,各个组节点中均包含了几何信息和用于控制其外观的渲染状态信息.根节点 ...

  4. android 动态改变屏幕方向

    LANDSCAPE与PORTRAIT 范例说明 要如何通过程序控制Activity的显示方向?在Android中,若要通过程序改变屏幕显示的方向,必须要覆盖 setRequestedOrientati ...

  5. yii 隐藏index.php的步骤

    Apache 1.开启apache的mod_rewrite模块       去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号     ...

  6. Sharepoint 2010 根据用户权限隐藏Ribbon菜单(利用css)

    本文介绍的是根据用户权限隐藏整个Ribbon菜单项. 操作环境:Windows Server 2008 r2+ SharePoint 2010 1.关于SharePoint  权限详细请参考:http ...

  7. dsp与sem的互补以及技术实现

    SEM翻译过来叫搜索引擎营销,个人认为是随着搜索引擎竞价排名出现的一个行业,已经有了好多年的历史,做sem的公司这些年里手里应该都积攒着大量的cookie,关键词等与SEM和追踪相关的数据,这些数据现 ...

  8. Miaomiao's Geometry

    HDU 4932  Bestcoder Problem Description There are N point on X-axis . Miaomiao would like to cover t ...

  9. 大文件读取方法(C#)

    之前都是用StreamReader.ReadLine方法逐行读取文件,自从.NET4有了File.ReadLines这一利器,就再也不用为大文件发愁了. File.ReadLines在整个文件读取到内 ...

  10. Debian 7.4 中配置PHP环境

    准备工作 导入密钥 wget http://www.dotdeb.org/dotdeb.gpg sudo apt-key add dotdeb.gpg 添加源 vi /etc/apt/sources. ...