codeforces 440C. One-Based Arithmetic 解题报告
题目链接:http://codeforces.com/problemset/problem/440/C
题目意思:给出一个数你,问需要用到的1来组成这个数的最少数量是多少。
我一开始对每个数只从 “+”的方向找,也就是假设对于4873,由千位开始配1,接着从百位,然后十位,最后个位。具体过程:4444 ---> 4888 ---> 4877 ---> 4873。对于test 3 的72我就悲剧了。因为最少数量应该是111 - 3*11 = 78, 78 - 5*1 = 72(15个1即可);而不是11*7 = 77, 77 - 5*11 = 72(19个1)(我的方法正是后者),得不到最少数量。
正确做法应该用搜索来做。唉~~~我对于dfs中递归总是很头痛,本来想利用人家的代码来调试(我只会用VC6调)清楚,怎么知道老是穿插一些汇编代码,调到int r1 = dfs(Abs(num-p1*ones[d]), d-1); 就看不清楚了= =......
这个代码我只能看懂一部分,希望有能之士看明白之后可以提点提点^_^...不过也好,知道自己有什么薄弱的地方赶快弥补弥补,这个递归总是要弄明白的,先放下这道题,苦练搜索题:dfs + bfs!!!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- typedef long long LL;
- const int maxn = + ;
- LL ones[maxn], n;
- void Init()
- {
- ones[] = ;
- for (int i = ; i < ; i++)
- ones[i] = ones[i-] * + ;
- }
- LL Abs(LL tmp)
- {
- return (tmp < ? -tmp : tmp);
- }
- LL dfs(LL num, int d)
- {
- if (d == )
- return num;
- int p1 = num / ones[d];
- int p2 = p1 + ;
- int r1 = dfs(Abs(num-p1*ones[d]), d-);
- int r2 = dfs(Abs(num-p2*ones[d]), d-);
- return min(p1*(d+)+r1, p2*(d+)+r2);
- }
- int main()
- {
- while (scanf("%lld", &n) != EOF)
- {
- Init();
- cout << dfs(n, ) << endl;
- }
- return ;
- }
调试版(VC6)(读者请忽略)
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- const int maxn = + ;
- __int64 ones[maxn], n;
- void Init()
- {
- ones[] = ;
- for (int i = ; i < ; i++)
- ones[i] = ones[i-] * + ;
- //for (int i = 0; i < 16; i++)
- // cout << "ones[" << i << "] = " << ones[i] << endl;
- }
- __int64 Min(__int64 a, __int64 b)
- {
- return (a > b ? b : a);
- }
- __int64 Abs(__int64 tmp)
- {
- return (tmp < ? -tmp : tmp);
- }
- __int64 dfs(__int64 num, int d)
- {
- if (d == )
- return num;
- int p1 = num / ones[d];
- int p2 = p1 + ;
- int r1 = dfs(Abs(num-p1*ones[d]), d-);
- int r2 = dfs(Abs(num-p2*ones[d]), d-);
- return Min(p1*(d+)+r1, p2*(d+)+r2);
- }
- int main()
- {
- while (scanf("%I64d", &n) != EOF)
- {
- Init();
- printf("%I64d\n", dfs(n, ));
- }
- return ;
- }
codeforces 440C. One-Based Arithmetic 解题报告的更多相关文章
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
- codeforces 460A Vasya and Socks 解题报告
题目链接:http://codeforces.com/problemset/problem/460/A 题目意思:有一个人有 n 对袜子,每天早上会穿一对,然后当天的晚上就会扔掉,不过他会在 m 的倍 ...
- codeforces 567D.One-Dimensional Battle Ships 解题报告
题目链接:http://codeforces.com/problemset/problem/567/D 题目意思:给出 1 * n 的 field,编号从左至右依次为 1,2,...,n.问射 m 枪 ...
随机推荐
- CentOS 7 使用iptables 开放端口
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service system ...
- 简单说明PHP的垃圾收集机制是怎样的?
腾讯 对变量有个引用计数,计数到0时变量被销毁. ———————————————————————— 每一种语言都有自己的自动垃圾回收机制,让程序员不必过分关心程序内存分配,但是在OOP中,有些对象需要 ...
- Delphi使用进行post数据时超时设置
因项目需要进行http的post提交数据,开始时用indy的idHttp组件,但是测试时发现当网络中断(如拔掉网线),idHttp的超时设置无效果,要等20秒才提示超时(参考网上的做法,将indy9升 ...
- webstorm(二):拼写warning
逼死强迫症之对拼写进行检查,警告 typo:in word “msgfromfather”
- 高性能mysql之schema与数据类型优化
1.数据类型 http://www.cnblogs.com/YDDMAX/p/4937770.html
- [Bzoj3205][Apio2013]机器人(斯坦纳树)(bfs)
3205: [Apio2013]机器人 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 977 Solved: 230[Submit][Status] ...
- Java Enum枚举的用法(转)
说明:Java的枚举比dotnet的枚举好用,至少支持的方式有很多. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以 ...
- How to resolve 'Potential Leak' issue
-1 down vote favorite I am using the 'analyze' tool in xcode to check for potential leakages in my a ...
- Java中的Copy-on-Write容器 & ConcurrentHashMap & HashTable比较
参考这篇文章:Link 从JDK1.5开始Java并发包里提供了两个使用CopyOnWrite机制实现的并发容器,它们是CopyOnWriteArrayList和CopyOnWriteArraySet ...
- fetch 函数分装
1.fetch /** * 封装 fetch */ import { hashHistory } from 'react-router'; export default function reques ...