• 6.25AtCoderABC257E - Addition and Multiplication 2题解


    6.25AtCoderABC257E - Addition and Multiplication 2题解

    题目描述

    链接

    AtCoderABC257E - Addition and Multiplication 2

    文字描述

    E - Addition and Multiplication 2 /
    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 500 points

    Problem Statement
    Takahashi has an integer x. Initially, x=0.

    Takahashi may do the following operation any number of times.

    Choose an integer i (1≤i≤9). Pay C
    i

    yen (the currency in Japan) to replace x with 10x+i.
    Takahashi has a budget of N yen. Find the maximum possible value of the final x resulting from operations without exceeding the budget.

    Constraints
    1≤N≤10
    6

    1≤C
    i

    ≤N
    All values in input are integers.
    Input
    Input is given from Standard Input in the following format:

    N
    C
    1

    C
    2

    … C
    9

    Output
    Print the answer.

    Sample Input 1
    Copy
    5
    5 4 3 3 2 5 3 5 3
    Sample Output 1
    Copy
    95
    For example, the operations where i=9 and i=5 in this order change x as:

    0→9→95.

    The amount of money required for these operations is C
    9

    +C
    5

    =3+2=5 yen, which does not exceed the budget. Since we can prove that we cannot make an integer greater than or equal to 96 without exceeding the budget, the answer is 95.

    Sample Input 2
    Copy
    20
    1 1 1 1 1 1 1 1 1
    Sample Output 2
    Copy
    99999999999999999999
    Note that the answer may not fit into a 64-bit integer type.

    题目分析

    1. 此题想让数最大,最大首先保证位数最大,即用最小的数把k除向下取整的值就是,最大数的数位长度。
    2. 在位数最大的基础上,保证首位尽可能的大,但不能影响到取得数位长度。(经典的贪心)
    3. 由此推出每次先从首位挑尽可能大的,判断条件:当取到这位是其余为确定位能不能都填最小的(数位不变)。

    代码实现

    #include<bits/stdc++.h>
    using namespace std;
    
    int k,mi=1e9,n;
    int a[100];
    int main(){
    	scanf("%d",&k);
    	for(int i=1;i<=9;i++){
    		scanf("%d",&a[i]);
    		mi=min(mi,a[i]);
    	}
    	n=k/mi;
    	for(int i=1;i<=n;i++){
    		for(int j=9;j>=1;j--){
    			if(k-a[j]>=mi*(n-i)){
    				k-=a[j];
    				printf("%d",j);
    				break;
    			}
    		}
    	}
    	printf("\n");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    JAVA中JPanel类方法汇总
    合并后的以太坊会像一个流域
    【JAVA-Day38】深入了解Java常用类 String:字符串操作的技巧和方法
    js之原型链
    【C++】STL —— list的基本使用
    kubeadm安装k8s集群
    2022年东湖科学城建设扶持政策申报奖励补贴标准以及认定条件汇总
    ZYNQ LWIP实验
    hidl hwbinder和binder混合使用相关的joinThreadPool问题解答
    关于Java并发多线程的一点思考
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/125510092