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为各个位置上字符 ...
随机推荐
- 三维重建:SFM中BA的并行化
1. BA在重建中的作用 借鉴于运动中重建的方法,BA引入SLAM过程,而传统的滤波方法引入BA是跟随闭环检测出现. 1.1 BA在滤波方法中的嵌入 PTAM 1.2 BA在闭环检测之后的应用 在三维 ...
- (转)OpenLayers3基础教程——OL3基本概念
http://blog.csdn.net/gisshixisheng/article/details/46756275 OpenLayers3基础教程——OL3基本概念 从本节开始,我会陆陆续续的更新 ...
- Linux 之secureCRT连接SSH
1.登陆linux系统,打开终端命令.输入 rpm -qa |grep ssh 查找当前系统是否已经安装. 2.如果没有安装SSH软件包,可以通过yum 或rpm安装包进行安装. .3.安装好了之后 ...
- Zend Studio快捷键汇总
应用场景 快捷键 功能 查看快捷键 ctrl+shift+l 显示所有快捷键列表 查看和修改快捷键 打开Window->Preferences->General->keys 修改 ...
- 什么是hashMap,初始长度,高并发死锁,java8 hashMap做的性能提升
问题1:HashM安排的初始长度,为什么? 初始长度是 16,每次扩展或者是手动初始化,长度必须是 2的幂. 因为: index = HashCode(Key) & (length - 1), ...
- dev的汉化
dev汉化网上有很多介绍.自己感觉还是用ini文件,用Localization Component来的简单方便. 但是根据其16.1.6的demo死活出问题.原因是ini文件的编码问题.转换到位自然就 ...
- netty 拆包和粘包 (三)
在tcp编程底层都有拆包和粘包的机制 拆包 当发送数据量过大时数据量会分多次发送 以前面helloWord代码为例 package com.liqiang.nettyTest2; public c ...
- Spring MVC-页面重定向示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_page_redirection.htm 说明:示例基于Spring MVC 4. ...
- Sublime Text 3常用插件—Emmet
原文链接:http://www.cnblogs.com/easy-blue/p/6617852.html 摘要: 安装请看上一篇Sublime Text—安装,和sublime自带快捷键一起用,写ht ...
- LINUX内核内存屏障
================= LINUX内核内存屏障 ================= By ...