AtCoder Beginner Contest 155 E.Payment

In the Kingdom of AtCoder, only banknotes are used as currency. There are
10100+110^{100}+110100+1 kinds of banknotes, with the values of 1,10,102,103,,1010100.1,10,10^2,103,…,10^{10^{100}}.1,10,102,103,,1010100. 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,00010^{1,000,000}101,000,000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
  • 1

Output

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

Sample Input 1

36
  • 1

Sample Output 1

8
  • 1

Sample Input 2

91
  • 1

Sample Output 2

3
  • 1

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170
  • 1

Sample Output 3

243


题意:

在AtCoder王国中,仅将纸币用作货币。(10的次幂)

您来过商场购物,现在正在购买价值为N的章鱼烧机器。(章鱼烧是日本小吃的名称。)要付款,您需要选择至少N的金额,然后给 交给店员。 然后,业务员将找回的零钱返还给您,这是您给的金额减去N。

当您和店员都选择钞票组合以最小化此数量时,您和店员使用的最小总纸币数是多少?

假设您有足够数量的钞票,店员也有。

约束N是1到101,000,00010之间的整数
1,000,000(含)。

 

如果您给每张四张面额为10的纸币,店员给您四张每张面额为1的纸币,则总共要使用八张钞票。
付款总额不能少于8张钞票,因此答案为8。

思路:

针对某一位数x,我们用ans直接记录每一位所需的钞票数,用res记录进位所需的钞票数,若
1.x 小于5,则答案直接加上 x
2.x 大于5,则答案直接加上 10−x
3.比较难考虑的就是 x=5 的情况,你说四舍五入,那么假设 n=5 时就不对,你如果进位的话就是一共付了6张,正确答案是5张;你说不进,那么假设 n=995,你进位后就是 1005,一共付了7张,正确答案是6张。通过我的举例你不难发现,x=5要进位只需它的前一位大于等于5即可

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<utility>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int maxn=2e5+10;
const int mod =1e9+7;
const double EPS = 1e-6;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    s='0'+s;
    ll ans=0;
    for(int i=s.size()-1;i>=1;i--)
    {
        if(s[i]>'5')
        {
            ans+=10-(s[i]-'0');
            s[i-1]++;
        }
        else if(s[i]=='5'&&s[i-1]>='5')
        {
            ans+=10-(s[i]-'0');
            s[i-1]++;
        }
        else
        {
            ans+=(s[i]-'0');
        }
    }
    if(s[0]!='0')
        ans++;
    cout << ans << endl;
    return 0;
}

 

posted @ 2020-02-17 23:31  晴天要下雨  阅读(211)  评论(0编辑  收藏  举报