Codeforces Round #356 D.Bear and Tower of Cubes DFS
2016-06-10 22:16
459 查看
D. Bear and Tower of Cubes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3.
A tower consisting of blocks with sides a1, a2, ..., ak has
the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X — the required total volume of the tower. Then,
Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m.
Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≤ m that results this number of
blocks.
Input
The only line of the input contains one integer m (1 ≤ m ≤ 1015),
meaning that Limak wants you to choose X between 1 and m,
inclusive.
Output
Print two integers — the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number
of blocks.
Examples
input
output
input
output
题意:有一个体积为m的塔,要你尽可能找最大的p使得p^3<=M,最后使得该塔尽可能地被填满在总体积在不大于m的条件下尽可能最大,其次从而使得用的立方体数目最多。
思路:dfs分别去考虑当前最大的p是否用上了,用上了则当前体积为m-p^3,反之体积变为p^3-1,这样才使得p不会被用上。
代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3.
A tower consisting of blocks with sides a1, a2, ..., ak has
the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X — the required total volume of the tower. Then,
Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m.
Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≤ m that results this number of
blocks.
Input
The only line of the input contains one integer m (1 ≤ m ≤ 1015),
meaning that Limak wants you to choose X between 1 and m,
inclusive.
Output
Print two integers — the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number
of blocks.
Examples
input
48
output
9 42
input
6
output
6 6
题意:有一个体积为m的塔,要你尽可能找最大的p使得p^3<=M,最后使得该塔尽可能地被填满在总体积在不大于m的条件下尽可能最大,其次从而使得用的立方体数目最多。
思路:dfs分别去考虑当前最大的p是否用上了,用上了则当前体积为m-p^3,反之体积变为p^3-1,这样才使得p不会被用上。
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<string> #include<bitset> #include<utility> #include<functional> #include<iomanip> #include<sstream> #include<ctime> using namespace std; #define maxn int(1e3+20) #define inf int(0x3f3f3f3f) #define mod int(1e9+7) #define PI acos(-1.0) typedef long long ll; typedef pair<ll ,ll> point; ll get_max(ll n) { ll L = 1,R = ll(1e5+10),mid,ans = 1; while(L <= R) { mid = (R + L)/2LL; if(mid*mid*mid <= n){ L = mid+1; ans = mid; } else{ R = mid-1; } } return ans; } ll get_pow(ll p) { return p*p*p; } point ans; void solve(ll m,ll sum,ll num) { if(m == 0){ ans = max(ans,make_pair(num,sum)); return; } ll ma = get_max(m); solve(m - get_pow(ma),sum+get_pow(ma),num+1); if(ma-1 >= 0){ solve(get_pow(ma) - get_pow(ma-1) -1,sum+get_pow(ma-1),num+1); } } int main() { #ifdef CDZSC_June freopen("t.txt", "r", stdin); #endif ll a; while(~scanf("%lld",&a)) { ans = make_pair(0,0); solve(a,0,0); printf("%lld %lld\n",ans.first,ans.second); } return 0; }
相关文章推荐
- Spring MVC配置DispatcherServlet的url-pattern
- 网页编码就是那点事
- kaggle 入门 rossmann xgboost
- 彻底弄清楚session是什么?
- 120. Triangle
- ROS SLAM代码框架概述
- 写给自己的一封信(http://www.cnblogs.com/yejiurui/)
- hdu5464 Clarke and problem (dp)
- eclipse java spark作业提交
- 数据库知识整理<一>
- git使用详细介绍
- 117. Populating Next Right Pointers in Each Node II
- git将本地仓库上传到远程仓库
- CentOS yum有时出现“Could not retrieve mirrorlist ”的解决办法――resolv.conf的配置
- Tesseract-OCR 3.04在Windows7 vs2013上编译过程
- Tesseract-OCR 3.04在Windows7 vs2013上编译过程
- 自动移除的通知中心
- 模拟登陆百度之分析网站流程
- Hibernate 的学习之路之错误笔记day02
- pcl在vs2010上的配置(无cmake版)