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 枪 ...
随机推荐
- msp430入门学习40
msp430的其他八 msp430入门学习
- JFinal Weixin 1.6发布【转】
原文:http://www.oschina.net/news/69495/jfinal-weixin-1-6-released#rd 继JFinal 2.1发布之后,再来一发JFinal Weixin ...
- Wannafly挑战赛11 D 题 字符串hash + 卡常
题目链接 https://ac.nowcoder.com/acm/contest/73#question map与order_map https://blog.csdn.net/BillCYJ/art ...
- Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API...
问题描述:Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API... 解决方法:这种错误是eclips ...
- BS版代码生成器 简介
自用的代码生成器 核心:JdbcTemplate+freemarker 目前支持sqlserver和mysql 演示: 1.首界面 2.读数据库中的所有表 3.打开某数据库下的所有表.视图.存储过程 ...
- 如何删除xcode启动主页面项目列表
Open Xcode, leave the splash screen up and choose "File", "Open Recent Projects" ...
- Go -- 一致性哈希算法
一致性哈希算法在1997年由麻省理工学院的Karger等人在解决分布式Cache中提出的,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用 ...
- git 更新远程分支
使用git的时候,有时候会出现远端更新了一个分支,但是从本地想checkout一个远程分支时,会出现如下错误: fatal: git checkout: updating paths is incom ...
- BUPT复试专题—哈夫曼树(2010)
https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage= ...
- 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs
MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...