CodeForces 679B(Bear and Tower of Cubes)
题意: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)的更多相关文章
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- Codeforces 680D - Bear and Tower of Cubes
680D - Bear and Tower of Cubes 思路:dfs+贪心,设剩余的体积为res,存在a,使得a3 ≤ res,每次取边长为a的立方体或者边长为a-1的立方体(这时体积上限变成a ...
- 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 ...
- 【CodeForces】679 B. Bear and Tower of Cubes
[题目]B. Bear and Tower of Cubes [题意]有若干积木体积为1^3,2^3,...k^3,对于一个总体积X要求每次贪心地取<=X的最大积木拼上去(每个只能取一次)最后总 ...
- 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 ...
- 【19.05%】【codeforces 680D】Bear and Tower of Cubes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Bear and Tower of Cubes Codeforces - 680D
https://codeforces.com/contest/680/problem/D 一道2D,又是搞两个小时才搞出来...不过好在搞出来了 官方题解:可以证明对于m,设a是满足a^3<=m ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- uva 10051 Tower of Cubes(DAG最长路)
题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...
随机推荐
- CDOJ 482 Charitable Exchange bfs
Charitable Exchange Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...
- Codeforces Gym 100637A A. Nano alarm-clocks 前缀和
A. Nano alarm-clocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/p ...
- Visio Premium 2010密钥+破解激活方法
Visio Premium 2010密钥+破解激活方法: 在安装时能够使用下面密钥: GR24B-GC2XY-KRXRG-2TRJJ-4X7DC VWQ6G-37WBG-J7DJP-CY66Y-V27 ...
- delphi Sender和Tag的用法1
Sender和Tag的用法 在它们共同的OnClick事件下返回单击的那个按钮的标题 unit Unit1;interfaceuses Winapi.Windows, Winapi ...
- MySQL Troubleshoting:Waiting on query cache mutex 腾讯数据库工程师:幕南风
http://blog.itpub.net/26515977/viewspace-1208188/ 今天被MySQL Query Cache 炕了.线上大量 Waiting on ...
- 在Linux使用mingw32来编写win32程序
MinGW - Minimalist GNU For Windows Mingw32 是 GNU 計畫工具的集合,包含了大量的標頭檔(header files).函式庫與指 令程式.目的在提供免費的工 ...
- debian7 oracle11g 解决 link binaries 错误方案
ln -s /etc /etc/rc.d ln -s /usr/bin/awk /bin/awk ln -s /usr/bin/basename /bin/basename ln -s /usr/bi ...
- centos、linux改变ll命令显示颜色
服务器用的centOS,用putty管理时,在命令行下目录用蓝色显示,很难看清.可以用以下两种方法改变: 方法一: 1.直接修改个人帐户目录下的.color.rc文件. 找到 DIR 01;34,修改 ...
- selenium python 环境搭建(64位 windows)
之前写了同样的文章,可是后来自己按照给文章再次搭建环境当搭建环境成功后却发现还是无法用.使用from selenium import webdriver,在run的时候却出现ImportError: ...
- B - 一行盒子
Description 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子 ...