Benelux Algorithm Programming Contest 2019 E. Efficient Exchange(dp)

You have recently acquired a new job at the Bank for Acquiring Peculiar Currencies. Here people can make payments, and deposit or withdraw money in all kinds of strange currencies. At your first day on the job you help a customer from Nijmegia, a small insignificant country famous for its enormous coins with values equal to powers of 10, that is, 1, 10, 100, 1000, etc. This customer wants to make a rather large payment, and you are not looking forward to the prospect of carrying all those coins to and from the vault.

You therefore decide to think things over first. You have an enormous supply of Nijmegian coins in reserve, as does the customer (most citizens from Nijmegia are extremely strong). You now want to minimize the total number of coins that are exchanged, in either direction, to make the exact payment the customer has to make.

For example, if the customer wants to pay 83 coins there are many ways to make the exchange. Here are three possibilities:

  • Option 1. The customer pays 8 coins of value 10, and 3 coins of value 1. This requires exchanging 8 + 3 = 11 coins.

  • Option 2. The customer pays a coin of value 100, and you return a coin of value 10, and 7 coins of value 1. This requires exchanging 1 + 1 + 7 = 9 coins.

  • Option 3. The customer pays a coin of value 100, and 3 coins of value 1. You return 2 coins of value 10. This requires exchanging 1 + 3 + 2 = 6 coins.

It turns out the last way of doing it requires the least coins possible.

input

A single integer  the amount the customer from Nijmegia has to pay.

output

Output the minimum number of coins that have to be exchanged to make the required payment.

样例输入1复制

83

样例输出1复制

6

样例输入2复制

13

样例输出2复制

4

样例输入3复制

12345678987654321

样例输出3复制

42

我太菜了我按 > 5,= 5,<5 分的类WA了一下午……千算万算没有算到这是个dp 还是太菜了

 

题意:

有面值为10的幂大小的硬币,给定价格,问最少用到的硬币数量

思路:

每一位上的数字有两种情况:直接给 或 在前一位多给1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;

int dp[N][N];

int main()
{
    string s;
    while(getline(cin, s))
    {
        int len = s.size();
        dp[0][0] = 0;
        dp[0][1] = 1;
        for(int i = 0; i < len; ++i)
        {
            dp[i + 1][0] = min(dp[i][0] + s[i] - '0', dp[i][1] + 10 -s[i] + '0');
            dp[i + 1][1] = min(dp[i][0] + s[i] - '0' + 1, dp[i][1] + 9 - s[i] + '0');
        }
        cout<<dp[len][0]<<'\n';;
    }
    return 0;
}

 

全部评论

相关推荐

03-03 23:42
复旦大学 Java
tongx_:闹呢,这找不到其他人还活不活
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6395次浏览 61人参与
# 你的实习产出是真实的还是包装的? #
1304次浏览 32人参与
# 米连集团26产品管培生项目 #
4719次浏览 206人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7077次浏览 37人参与
# 简历第一个项目做什么 #
31329次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186521次浏览 1115人参与
# MiniMax求职进展汇总 #
23220次浏览 302人参与
# 研究所笔面经互助 #
118787次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309572次浏览 4163人参与
# 职能管理面试记录 #
10722次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62719次浏览 748人参与
# 网易游戏笔试 #
6374次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6993次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160437次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362741次浏览 2632人参与
# 你怎么看待AI面试 #
179417次浏览 1182人参与
# 小红书求职进展汇总 #
226905次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92146次浏览 896人参与
# 校招笔试 #
467654次浏览 2954人参与
# 经纬恒润求职进展汇总 #
155713次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务