Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接
题意
求一个不超过 \(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的更多相关文章
- Codeforces 680D - Bear and Tower of Cubes
680D - Bear and Tower of Cubes 思路:dfs+贪心,设剩余的体积为res,存在a,使得a3 ≤ res,每次取边长为a的立方体或者边长为a-1的立方体(这时体积上限变成a ...
- CodeForces 679B(Bear and Tower of Cubes)
题意:Limak要垒一座由立方体垒成的塔.现有无穷多个不同棱长(a>=1)的立方体.要求:1.塔的体积为X(X<=m).2.在小于X的前提下,每次都选体积最大的砖块.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 ...
- 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的最大积木拼上去(每个只能取一次)最后总 ...
- 【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 639E - Bear and Paradox(二分+贪心)
Codeforces 题目传送门 & 洛谷题目传送门 原来 jxd 作业里也有我会做的题 i 了 i 了 首先这种题目的套路就是先考虑对于一个固定的 \(c\),怎样求出得分最高的策略,而类似 ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
随机推荐
- ubuntu 16.04 安装QT问题
使用 sudo sh ./**.run 有错误: 增加 文件的可运行权限: sudo chmod +x Qt.run 直接运行: ./Qt.run 可完成安装
- Android开发之拍照功能实现
参考链接:http://www.linuxidc.com/Linux/2013-11/92892p3.htm 原文链接:http://blog.csdn.net/tangcheng_ok/articl ...
- C++的Android接口---配置NDK
一. 在安卓工具网站下载ADT:http://tools.android-studio.org/index.php 参考链接:http://1527zhaobin.iteye.com/blog/186 ...
- css3基础篇三
CSS3 文本阴影 在 CSS3 中,text-shadow 可向文本应用阴影. 您能够规定水平阴影.垂直阴影.模糊距离,以及阴影的颜色: 实例 向标题添加阴影: h1 { text-shadow: ...
- C# --MVC实现简单上传下载
首先创建一个默认的控制器Defaultcontroller 然后生成视图View 在视图里面 创建文件选择器 创建上传.下载按钮 代码如下 <body> <div> <f ...
- vue-router在同一个路由下切换,取不到变化的路由参数
最近用vue写项目的时候碰到一个问题,在同一个页面下跳转,路由地址不变,路由参数有变化,一开始只是在data里取路由的参数,发现根本取不到变化的路由参数. 例如:订单列表也跳转详情页,跳转方法如下 & ...
- [转载]windows下github 出现Permission denied (publickey).解决方法
今天在学习github的时候遇到了一些问题,然后爬了一会,找到了解决方法记录下来,以防忘记,当然能帮助别人最好啦! github教科书传送门:http://www.liaoxuefeng.com/ ...
- Ubuntu下解压(unzip)乱码的解决方法
在Windows上压缩的文件,是以Windows系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 通过unzip -- ...
- Docker 导入镜像报错:open /var/lib/docker/tmp/docker-import-743441288/redis-3.0.7/json: no such file or directory
下载好了redis的tar包,然后导入redis镜像是报错open /var/lib/docker/tmp/docker-import-743441288/redis-3.0.7/json: no s ...
- python-selenium,关于页面滑动
第一种: #滑到底部 js="var q=document.documentElement.scrollTop=100000" driver.execut_script(js) 目 ...