Payment(线性dp)

Payment(线性dp)

题目描述
In the Kingdom of AtCoder, only banknotes are used as currency. There are 10 100+1 kinds of banknotes, with the values of 1,10,10 2,10 3,…,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.
样例输入 Copy
【样例1】
36
【样例2】
91
【样例3】
314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170
样例输出 Copy
【样例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[i][0]表示第i位的答案,第i位不向前借位。dp[i][1]表示借位;
dp[i][0]=min(dp[i-1][0]+a[i],dp[i-1][1]+a[i]+1);
dp[i][1]=min(dp[i-1][0]+10-a[i],dp[i-1][1]+9-a[i]);
最后答案为min(dp[n][0],dp[n][1]+1)
#pragma GCC optimize(3 , "Ofast" , "inline")
 
#include <bits/stdc++.h>
 
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)
 
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int> pi;
 
template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}
 
const int maxn = 1e6 + 10;
const int inf = int (1e9);
const ll INF = ll (1e18);
const double PI = acos (- 1);
const int mod = 1e9+7;
const double eps = 1e-8;
 
 
string s;
ll dp[maxn][2];
int main () {
    cin>>s;
    reverse (s.begin (),s.end ());
    string t = "0"+s;
    dp[1][0]=t[1]-'0';
    dp[1][1]=10-(t[1]-'0');
    int l = s.size ();
    rep (i,2,l) {
        int x = t[i]-'0';
        dp[i][0]=min (dp[i-1][0]+x,dp[i-1][1]+x+1);
        dp[i][1]=min (dp[i-1][0]+10-x,dp[i-1][1]+9-x);
    }
    printf ("%lld\n",min (dp[l][0],dp[l][1]+1));
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值