zoukankan      html  css  js  c++  java
  • nyoj 791——Color the fence——————【贪心】

    Color the fence

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
     
    描述

    Tom has fallen in love with Mary. Now Tom wants to show his love and write a number on the fence opposite to 

    Mary’s house. Tom thinks that the larger the numbers is, the more chance to win Mary’s heart he has.

    Unfortunately, Tom could only get V liters paint. He did the math and concluded that digit i requires ai liters paint. 

    Besides,Tom heard that Mary doesn’t like zero.That’s why Tom won’t use them in his number.

    Help Tom find the maximum number he can write on the fence.

     
    输入
    There are multiple test cases.
    Each case the first line contains a nonnegative integer V(0≤V≤10^6).
    The second line contains nine positive integers a1,a2,……,a9(1≤ai≤10^5).
    输出
    Printf the maximum number Tom can write on the fence. If he has too little paint for any digit, print -1.
    样例输入
    5
    5 4 3 2 1 2 3 4 5
    2
    9 11 1 12 5 8 9 10 6
    样例输出
    55555
    33


    题意:给你一个v升油漆,给出数字1--9对应的油漆花费,让你用那么多油漆画出最大的数。

    解题思路;

    /*
    这题的贪心思想比较明显。我们要让能得到数尽量大,那么就要保证让位数尽量长,在位数
    尽量长的基础上,让高位数尽量大,这是我们的贪心策略。我们知道,除以最小的数,可以
    让位数最长,然而最长可以不仅仅是由最小的数得到。如:v=5 a={2,3,24,32,31,14,15,7,9},
    虽然可以得到最长长度是2,且由除以2可得到结果11,但是最优的结果却是21。所以只要我们能
    保证我们得到的高位数字比选择最小的数所得高位数字大并且位数相等即可。
    */

    #include<bits/stdc++.h>
    using namespace std;
    const int INF=1e9;
    int digit[20];
    int main(){
        int n,i,v,j,k,minv,ai;
        while(scanf("%d",&v)!=EOF){
            minv=INF;
            for(i=1;i<=9;i++){
                scanf("%d",&digit[i]);
                minv=minv>digit[i]?digit[i]:minv;
            }
            if(minv>v){
                printf("-1
    ");
                continue;
            }
            for(i=v/minv;i>=1;i--){//目前的染料如果染最小花费那个数字可以染多少位
                for(j=9;j>=1;j--){  //从大到小遍历数字
                    if(v>=digit[j]&&(v-digit[j])/minv+1>=i){
                            //目前的染料能染较大的数字且保证跟染最小那个数字可染的位数相同
                            //贪心选择染大的数字
                        printf("%d",j);
                        v-=digit[j];
                    }
                }
            }printf("
    ");
        }
        return 0;
    }
    

      



  • 相关阅读:
    02_Python基础
    python 学习网站
    Docker搭建MySQL服务
    Ubuntu源、Python虚拟环境及pip源配置
    Ubuntu18.04完全卸载mysql5.7并安装mysql8.0的安装方法
    MySQL 数据库
    数据库连接池
    ssm整合开发的相关步骤和增删改查
    Springmvc执行流程
    资源映射之----在springmvc中如何访问WEB-INF目录下的文件和系统文件夹中的文件
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4632475.html
Copyright © 2011-2022 走看看