Educational Codeforces Round 116 (Rated for Div. 2), problem: (C) Banknotes

传送门 Problem - C - Codeforces 

 题目

题目重点内容手打翻译:(欢迎批评指正)

在柏林, 使用着n套不同的货币(banknotes)。第i个货币面额为10ai 元,货币的第一种只能是1。定义f(s)为需要s元的最小货币数量,例如货币面额分别为1,10,100,然后f(59) = 14 :因为 九个面额1元的货币和五个面额10元的货币,也就是9*1+5*10=59元,并且没有办法使用更少的货币数量

 

 思路

尽量选择小面值的, 还不能被其它货币代替的, 也就是10ai+1  - 10ai - 1, 题目样例很良心, 可以猜出来, 部分解释在代码里

 

 AC代码

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
const int N = 1e6;
LL money[15];
int a[20];

void init()
{
    money[0] = 1;//money是10的i次方
    for(int i = 1; i <= 10; i ++)
        money[i] = money[i-1]*(LL)10;
}
int main(){
    int t, n, k;
    
    init();
    cin >> t;
    while(t --)
    {
        cin >> n >>k;
        k ++;//k是货币数量, +1因为最后要让他k个货币无法达到这些钱(res)
        for(int i = 0; i < n; i ++)    cin >> a[i];
        
        sort(a, a+n);
        
        LL res = 0;
        for(int i = 0; i < n; i ++)
        {
            if(i == n-1){//特判, 多写点这些就不用考虑最后了
                res += (LL)k * money[a[i]];
                break;
            }

        //chaju就是差距,比如1和10差9,1和100差99,代表本回合最多能加多少个a[i] LL chaju
= 0, tt = a[i+1]-a[i]; chaju = money[tt] - 1; if(k > chaju){ k -= chaju; res += (LL)chaju * money[a[i]]; } else{ res += (LL)k * money[a[i]]; break; } } cout << res <<endl; } return 0; }

 

posted @ 2021-11-11 21:52  la-la-wanf  阅读(36)  评论(0编辑  收藏  举报