QOJ.ac

QOJ

時間限制: 3 s 記憶體限制: 2048 MB 總分: 100

#6257. 高效交换

统计

你最近在“奇特货币收购银行”找到了一份新工作。在这里,人们可以进行支付,或者存取各种奇怪的货币。在你上班的第一天,你接待了一位来自 Nijmegia 的顾客。Nijmegia 是一个不起眼的小国,以其面值为 10 的幂(即 $1, 10, 100, 1000$ 等)的巨大硬币而闻名。这位顾客想要进行一笔相当大的支付,而你一想到要从金库里搬运那么多硬币就感到头疼。

因此,你决定先仔细考虑一下。你和顾客都有充足的 Nijmegian 硬币储备(大多数 Nijmegia 公民都非常强壮)。你现在想要最小化为了完成顾客必须支付的精确金额而交换的硬币总数(无论支付还是找零)。

例如,如果顾客想要支付 $83$ 个单位的金额,有多种交换方式。以下是三种可能性:

  • 方案 1:顾客支付 $8$ 枚面值为 $10$ 的硬币和 $3$ 枚面值为 $1$ 的硬币。这需要交换 $8 + 3 = 11$ 枚硬币。
  • 方案 2:顾客支付 $1$ 枚面值为 $100$ 的硬币,你找回 $1$ 枚面值为 $10$ 的硬币和 $7$ 枚面值为 $1$ 的硬币。这需要交换 $1 + 1 + 7 = 9$ 枚硬币。
  • 方案 3:顾客支付 $1$ 枚面值为 $100$ 的硬币和 $3$ 枚面值为 $1$ 的硬币,你找回 $2$ 枚面值为 $10$ 的硬币。这需要交换 $1 + 3 + 2 = 6$ 枚硬币。

事实证明,最后一种方式所需的硬币数量最少。

输入格式

  • 一个整数 $0 \le n < 10^{1000}$,表示来自 Nijmegia 的顾客需要支付的金额。

输出格式

  • 输出为完成支付所需交换的最小硬币数量。

样例

输入格式 1

83

输出格式 1

6

输入格式 2

13

输出格式 2

4

输入格式 3

0

输出格式 3

0

输入格式 4

12345678987654321

输出格式 4

42

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.