AtCoder 155 E Payment

Posted jony-english

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder 155 E Payment相关的知识,希望对你有一定的参考价值。

Payment

时间限制: 1 Sec  内存限制: 128 MB

题目描述

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10100+1 kinds of banknotes, with the values of 1,10,102,103,…,10(10^100). You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints
·N is an integer between 1 and 101,000,000 (inclusive).
 

输入

Input is given from Standard Input in the following format:

N
 

输出

Print the minimum possible number of total banknotes used by you and the clerk.

样例输入

【样例1】
36
【样例2】
91
【样例3】
314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

样例输出

【样例1】
8
【样例2】
3
【样例3】
243

提示

样例1解释
If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.
样例2解释
If you give two banknotes of value 100,1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

题解

一道dp,对于某一种纸币,假如需要支付x张,可以支付x张或10-x张+一张大一级的纸币。

技术图片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int N =1000009;
 6 typedef long long LL;
 7 char c[N];
 8 int f[N][2];
 9 int n;
10 int main()
11 {
12     memset(f, 0x3f, sizeof(f));
13     scanf("%s", c + 1);
14     n = strlen(c + 1);
15     f[n][0] = c[n] - 0;
16     f[n][1] = 10 - (c[n] - 0);
17     c[0] = 0;
18     for(int i = n - 1; i >= 0; i--)
19         f[i][0] = min(f[i+1][0] + c[i] - 0, f[i+1][1] + c[i] - 0 +1),
20         f[i][1] = min(f[i+1][0] + 10 - (c[i] - 0), f[i+1][1] + 10 - (c[i] - 0 + 1));
21     printf("%d
",min(f[0][0], f[0][1]));
22     return 0;
23 }
View Code

 

以上是关于AtCoder 155 E Payment的主要内容,如果未能解决你的问题,请参考以下文章

AtCoder ABC 155F Perils in Parallel

AtCoder ABC 155D Pairs

AtCoder 155 C Poll

AtCoder Beginner Contest 155 简要题解

Atcoder ARC101 E 树dp

AtCoder Beginner Contest 173 E - Multiplication 4 (思维)