【CF679B】Theseus and labyrinth(数学,贪心)
题意:
给一个m<=10^15,每次都减最接近当前值的立方数
让你找一个不大于m的最大的数并且这个数是减法次数最多的数
思路:见http://blog.csdn.net/miracle_ma/article/details/52458715
开始想用贪心直接写
后面发现步数是对的,但使原数最大很难处理,因为各个i^3之间i的差不都<=1
于是用DFS处理
以下是大神题解:
考虑第一块取多少,最大的a3≤m
如果取a,还剩m−a3
取a−1的话,那肯定最大的X是a3−1,剩下a3−1−(a−1)3
如果取a−2的话,肯定没有a−1来的优,因为剩下的比取a−1剩下的要少
所以就是取a或者a−1,然后对于每次剩下的都可以这么考虑
如果你问,剩下x的时候,不是应该要取最大的a么
所以我们开头假设的X,不一定是最终的X
最后根据你取的,重新安排开头的X
比如这会剩x,然后取a−1,x当作了a3−1−(a−1)3
那么只要在最开始的时候X取小一点就行了
所以dfs的时候记录个数,还剩多少,∑a3
var f:array[..]of qword;
n,ans1,ans2:qword;
i:longint; function clac(x:qword):qword;
var l,r,mid,last:qword;
begin
l:=; r:=trunc(sqrt(x)); last:=l;
while l<=r do
begin
mid:=(l+r)>>;
if mid*mid<=x div mid then begin last:=mid; l:=mid+; end
else r:=mid-;
end;
exit(last);
end; procedure dfs(s1,k,s2:qword);
var p:qword;
begin
if s1= then
begin
if (k>ans1)or((k=ans1)and(s2>ans2)) then begin ans1:=k; ans2:=s2; end;
exit;
end;
p:=clac(s1);
dfs(s1-f[p],k+,s2+f[p]);
if p> then dfs(f[p]--f[p-],k+,s2+f[p-]);
end; begin
// assign(input,'1.in'); reset(input);
//assign(output,'1.out'); rewrite(output);
readln(n);
for i:= to do begin f[i]:=i; f[i]:=f[i]*f[i]*f[i]; end;
ans1:=; ans2:=;
dfs(n,,);
writeln(ans1,' ',ans2);
//close(input);
//close(output);
end.
【CF679B】Theseus and labyrinth(数学,贪心)的更多相关文章
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
- 【25.93%】【676D】Theseus and labyrinth
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 洛谷3月月赛div2 题解(模拟+数学+贪心+数学)
由于本人太蒻了,div1的没有参加,胡乱写了写div2的代码就赶过来了. T1 苏联人 题目背景 题目名称是吸引你点进来的. 这是一道正常的题,和苏联没有任何关系. 题目描述 你在打 EE Round ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- HDOJ 5073 Galaxy 数学 贪心
贪心: 保存连续的n-k个数,求最小的一段方差... .预处理O1算期望. .. Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- FZU 2144 Shooting Game(数学+贪心)
主要思路:求出蚊子到达球的时间区间(用方程得解),对区间做一个贪心的选择,选择尽可能多的区间有交集的区间段(结构体排序即可),然后计数. #include <cstdio> #includ ...
- hdu 3573(数学+贪心)
Buy Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 5747(数学,贪心)
Aaronson Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心
C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- PMD 编译 语法分析 词法分析 抽象语法树
编译原理 163 课堂 http://mooc.study.163.com/learn/-1000002001?tid=1000003000#/learn/content?type=detail&am ...
- shell脚本,awk取奇数行与偶数行方法。
第一种方法: 第二种方法: 第三种方法:
- 瀑布流封装(仿写UITableView)
本篇文章将会仿照苹果系统提供的UITableView类,封装一个瀑布流效果的控件!!! 该控件和系统的UITableView是相同级别的 (继承自系统的UIScrollView) GitHub中Dem ...
- 【jenkins】jenkins执行nohup java报错
nohup:failed to run command 'java':No such file or directory 这是因为jenkins只认绝对路径.在shell里面有涉及到文件的都应该写成绝 ...
- Linux 安装Nginx+PHP+MySQL教程
一.安装nginx 通过yum安装openssl: yum -y install openssl openssl-devel 通过yum安装pcre: yum -y install pcre-deve ...
- jsp页面上传多个name值到后台
平常利用表单提交的一般都是一个文本框对应一个name,而在后台都是利用request.getParameter(String name);这段代码返回的是一个String类型的参数:而当我们页面上有多 ...
- android:exported属性
这个属性用于指示该服务是否能够被其他应用程序组件调用或跟它交互.如果设置为true,则能够被调用或交互,否则不能.设置为false时,只有同一个应用程序的组件或带有相同用户ID的应用程序才能启动或绑定 ...
- An entity cannot be annotated with both @Entity and @MappedSuperclass: com.example1.demo1.Entity.User错误
项目问SpringDataJpa项目,在运行的过程中出现的以下错误: Caused by: org.hibernate.AnnotationException: An entity cannot be ...
- spoj104 HIGH - Highways 矩阵树定理
欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...
- luogu3389 【模板】高斯消元法
#include <algorithm> #include <iostream> #include <cstdio> #include <cmath> ...