题意: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. iOS开发——网络编程Swift篇&(六)异步Post方式

    异步Post方式 // MARK: - 异步Post方式 func asynchronousPost() { //创建NSURL对象 var url:NSURL! = NSURL(string: &q ...

  2. Web开发接口测试工具——Postman插件的使用(chrome浏览器)

    Postman是chrome浏览器的一款插件.Postman 可以模拟 http 请求的发送,并自动解析 JSON 和 XML 的返回数据. 可以手动的去配置各类 parameter,还支持 Basi ...

  3. 关于使用NotificationComat导致android2.3及以下版本无法显示自定义布局的解决方法.

    大伙都知道 android-support-v4为我们提供了很多兼容的解决方案, 其中就有关于通知栏的. NotificationCompat, 顺利成章操刀显示通知. eg: Intent inte ...

  4. or1200下raw-os(仿真环境篇)

    貌似最近都在公司混日子过了,怎么办?哎哎哎~罪过啊罪过,不过也是的,加工资居然没我份,顶领导个肺的,叫我怎么继续活啊~哎哎哎~ 算了,不谈这些鸟事情了,说多了都是泪啊,这篇blog开始我们进入raw- ...

  5. 炼数成金hadoop视频干货01

    视频地址:http://pan.baidu.com/s/1dDEgKwD 最开始还是讲hadoop的起源,但是和其他垃圾视频不同,不是照本宣科,听了还是受益.作者给人一种感觉就是他是确实把他的经验和体 ...

  6. CollatingOfData 之 JsonHelper

    1 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System. ...

  7. ASP.NET 之 检测到在集成的托管管道模式下不适用的ASP.NET设置

    将ASP.NET程序从IIS6移植到IIS7后,调试运行可能提示以下错误: HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP. ...

  8. How to Diagnose Audi Vehicles via Tuirel S777

    Here share how to diagnose Audi cars via Tuirel S777. 1.Well connect Tuirel S777 to your Audi car, a ...

  9. Virtualbox - Fatal: Could not read from the boot medium; system halted!

    刚装好的虚拟机系统,重新打开Virtualbox时出现个什么提示没认真看,顺势点了下去......结果虚拟机的xp系统打不开了,启动后出现:Fatal: Could not read from the ...

  10. Composer PHP 依赖管理工具

    composer 是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件. 依赖管理 ...