Codeforces Round #356 (Div. 2) D DFS
2016-07-02 19:33
260 查看
链接:戳这里
D. Bear and Tower of Cubes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
The second added block has side 2, so the remaining volume is 15 - 8 = 7.
Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7·13 = 27 + 8 + 7 = 42.
题意:
对于给出的一座塔的体积m,求出最多的x^3的体积使得堆起来<=m,如果个数相同,输出堆出的最大体积
思路:
暴搜下去,二分找出当前最大的i*i*i<=m,然后两种选择,统计这个i的答案,统计这个(i-1)的答案。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll m;
pair<ll,ll> ans;
ll getmax(ll n){
int l=1,r=100000,mid,ans=-1;
while(l<r){
mid=(l+r)/2;
if((ll)mid*mid*mid<=n) {
l=mid+1;
ans=mid;
} else r=mid;
}
return ans;
}
ll val(int x){
return (ll)x*x*x;
}
void DFS(ll n,ll num,ll v){
if(n==0){
ans=max(ans,make_pair(num,v));
return ;
}
int tmp=getmax(n);
DFS(n-val(tmp),num+1,v+val(tmp));
if(tmp>=1){
DFS(val(tmp)-1-val(tmp-1),num+1,v+val(tmp-1));
}
}
int main(){
scanf("%I64d",&m);
DFS(m,0,0);
printf("%I64d %I64d\n",ans.first,ans.second);
return 0;
}
D. Bear and Tower of Cubes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
The second added block has side 2, so the remaining volume is 15 - 8 = 7.
Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7·13 = 27 + 8 + 7 = 42.
题意:
对于给出的一座塔的体积m,求出最多的x^3的体积使得堆起来<=m,如果个数相同,输出堆出的最大体积
思路:
暴搜下去,二分找出当前最大的i*i*i<=m,然后两种选择,统计这个i的答案,统计这个(i-1)的答案。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll m;
pair<ll,ll> ans;
ll getmax(ll n){
int l=1,r=100000,mid,ans=-1;
while(l<r){
mid=(l+r)/2;
if((ll)mid*mid*mid<=n) {
l=mid+1;
ans=mid;
} else r=mid;
}
return ans;
}
ll val(int x){
return (ll)x*x*x;
}
void DFS(ll n,ll num,ll v){
if(n==0){
ans=max(ans,make_pair(num,v));
return ;
}
int tmp=getmax(n);
DFS(n-val(tmp),num+1,v+val(tmp));
if(tmp>=1){
DFS(val(tmp)-1-val(tmp-1),num+1,v+val(tmp-1));
}
}
int main(){
scanf("%I64d",&m);
DFS(m,0,0);
printf("%I64d %I64d\n",ans.first,ans.second);
return 0;
}
相关文章推荐
- 学习-安全类网站
- 自定义栈的实现及使用两个栈模拟队列
- cocos2d-x-3.11.1编译时输出无法打开头文件thread
- ViewPager与底部小圆点的联动
- STM32F103学习笔记——外部中断
- JavaSE复习_12 Socket网络编程
- google perftools分析程序性能
- Spring MVC @SessionAttributes注解
- 测试数据的生成——faker、chance和json-schema-faker
- 别人的设计模式 文章
- linux shell 之if-------用if做判断
- 谈谈闭包中的 - 变量
- Android 上传文件,图片。以及服务器端接收相关(转载)。
- 1012. The Best Rank (25)
- bootstrap搜索框样式代码及效果
- 从IP地址的扩展
- 黑马程序员——String
- 编程之美 - 数组分割
- count(1) count(*)
- 为什么要用Executors.defaultThreadFactory().newThread(run);创建线程?