链接

Codeforces 680D Bear and Tower of Cubes

题意

求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超过当前剩余体积。问在能选最多个数的情况下,\(X\) 最大是多少

思路

对于每一次选择,首先要保证选完后的剩余体积最大,这样就保证了能选最多个数。然后在这基础上保证 \(X\) 最大。

考虑对于最大的 \(a\),使得 \(a^3<=m\).

如果当前选择的是 \(a\),则剩余体积就是 \(m1 = m - a^3\)

如果当前选择的是 \(a - 1\), 则剩余体积就是 \(m2 = (a^3 - 1) - (a - 1)^3\). 要保证 \(a-1\) 是可选的最大的,对 \(m\) 的上限有要求

如果当前选择的是 \(a - 2\), 则剩余体积就是 \(m3 = (a-1)^3 - 1 - (a - 2)^3\).

所以可以发现在 \(a>=1\) 的情况下, \(m2\) 恒不小于 \(m3\), 所以就不用考虑 \(a-2\) 了

这样对于每一个状态,只要考虑 \(a\) 和 \(a - 1\). 时间复杂度是 \(O(m^\frac{1}{3})\)

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string> #define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8
#define TRI_POW(x) (x) * (x) * (x) using namespace std; LL ans, res;
void work(LL left, LL cnt, LL cur){
if (left == 0){
if (cnt > ans){
ans = cnt;
res = cur;
}
return;
}
LL x = 1;
while (TRI_POW(x + 1) <= left){
++x;
}
work(left - TRI_POW(x), cnt + 1, cur + TRI_POW(x));
if (x > 0){
work(TRI_POW(x) - 1 - (TRI_POW(x - 1)), cnt + 1, cur + TRI_POW(x - 1));
}
}
int main(){
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
LL n;
while (~scanf("%I64d", &n)){
ans = 0;
work(n, 0, 0);
printf("%I64d %I64d\n", ans, res);
}
}

Codeforces 680D Bear and Tower of Cubes 贪心 DFS的更多相关文章

  1. Codeforces 680D - Bear and Tower of Cubes

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

  2. CodeForces 679B(Bear and Tower of Cubes)

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

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

  4. 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 ...

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

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

  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 639E - Bear and Paradox(二分+贪心)

    Codeforces 题目传送门 & 洛谷题目传送门 原来 jxd 作业里也有我会做的题 i 了 i 了 首先这种题目的套路就是先考虑对于一个固定的 \(c\),怎样求出得分最高的策略,而类似 ...

  9. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

随机推荐

  1. JavaScript编程题(一)

    使用Javascript脚板输出如图所示的效果页面: 使用document.write()输出水平线 使用循环控制每个水平线的长度 答案:<!doctype html> <html ...

  2. Windows下VS2013 C++编译测试faster-rcnn

    [原创帖!转载请注明出处:http://www.cnblogs.com/LaplaceAkuir/p/6445189.html] 本人最近研究faster-rcnn,在ubuntu成功跑通matlab ...

  3. 测试 Zoundry Raven

    安装很方便,看看发布的内容是否好用 但发现从博客上取下来的内容是有问题的,不能正常打开

  4. Swoole server函数列表(转载)

    swoole_server::__construct swoole_server::set swoole_server::on swoole_server::addlistener swoole_se ...

  5. eas之单据转换规则

    /**  * BOTP单据转换  * @param botpNum 转换规则编号  * @param BillInfo 原单据  */ public static void BOTP(String b ...

  6. 单点登录之 CAS SSO 从入门到精通(第一天)

    转自:http://blog.csdn.net/lifetragedy/article/details/43817903 啊......it's quite a long time. 好久没更新博客了 ...

  7. [bzoj3507 Cqoi2014]通配符匹配 (hash+DP)

    传送门 Solution 显然用哈希233 设\(f[i][j]\)表示第i个通配符和当前第j个字符是否匹配 考虑两种通配符的特性,直接转移即可 Code #include <cstdio> ...

  8. NYIST 914 Yougth的最大化

    Yougth的最大化时间限制:1000 ms | 内存限制:65535 KB难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗? ...

  9. WinForm 登录窗体 + 单实例运行

    关于怎么在winform里增加登录窗体或者如何让winform程序单实例运行网上都很多例子. 然而两者结合起来呢? //Program.cs static class Program { public ...

  10. 微信企业号开发:UserAgent

    userAgent 属性是一个仅仅读的字符串,声明了浏览器用于 HTTP 请求的用户代理头 的值.微信企业号的打开网页的userAgent又包括那些信息呢? 使用userAgent能够推断用户訪问的浏 ...