链接

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. 使用pelican创建静态博客

    创建工作目录 首先使用pip安装pelican和markdown pip install pelican markdown 然后创建目录 mkdir my_blog 接着进入目录cd my_blog, ...

  2. numpy安装失败-小失误

    1. 古老的方法:            安装python numpy库AMD64 失败,网上的教程是这样的:http://www.cnblogs.com/zhuyp1015/archive/2012 ...

  3. ui界面设计

    UI即User Interface(用户界面)的简称,指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位 ...

  4. jq 替换DOM layeui 不刷新

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Linux 重要文件目录

    文件系统层次化标准(Filesystem Hierarchy Standard)[FHS] 树形结构 /boot 开机所需文件——内核开机菜单以及所需的配置文件等 /dev 以文件形式存放任何设备与接 ...

  6. Day 18 hashlib,logging模块

    hashlib 模块 作用:hash是一种算法,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法,该算法接受传入的内容,经过运算得到一串hash值 特点: 1.只要 ...

  7. C++ 对象创建的问题

    一.C++对象的创建: 对象创建的注意事项: 1.对象数组里的个数,就是创建对象的个数,普通数组一样:下标从0 到数组里数字 -1: 2.类名*  对象指针   <-->  这里只是一个指 ...

  8. 5、SpringBoot+MyBaits+Maven+Idea+pagehelper分页插件

    1.为了我们平时方便开发,我们可以在同一个idea窗口创建多个项目模块,创建方式如下 2.项目中pom.xm文件的内容如下 <?xml version="1.0" encod ...

  9. 15.Hibernate一对多双向关联映射+分页

    1.创建如下数据库脚本 --创建用户信息表 --编号,用户名,密码,年龄,性别,昵称,手机,地址,管理员,图像地址 create table users ( id ) primary key, use ...

  10. BALNUM - Balanced Numbers

    BALNUM - Balanced Numbers Time limit:123 ms Memory limit:1572864 kB Balanced numbers have been used ...