码迷,mamicode.com
首页 > 其他好文 > 详细

AtCoder 155 E Payment

时间:2020-03-14 21:45:00      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:set   scan   cli   hellip   inpu   size   back   hang   ann   

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\n",min(f[0][0], f[0][1]));
22     return 0;
23 }
View Code

 

AtCoder 155 E Payment

标签:set   scan   cli   hellip   inpu   size   back   hang   ann   

原文地址:https://www.cnblogs.com/Jony-English/p/12494356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!