NYOJ-Color the fence

来源:互联网 发布:如何优化亚马逊关键词 编辑:程序博客网 时间:2024/05/16 15:05

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

题目要求输入总颜料,然后输入九个数字分别需要的颜料
输出可以刷的最大数

AC代码

#include <stdio.h>  int main(){    int v,a[10],i,j,k;    while(~scanf("%d",&v))    {        int min=10000000;        for(i=1;i<=9;i++)        {            scanf("%d",&a[i]);            if(a[i]<=min)            {                min=a[i];                k=i; //花费相等的,取靠后的数字大的              }        }        if(min>v)        {   printf("-1\n");            continue;        }// 最小的花费大于总颜料写不了        int len=v/min;//可以写的数字长度         int r=v%min;//剩余的不够写一个数字的颜料         if(r==0)        {            for(i=1;i<=len;i++)                printf("%d",k);            printf("\n");            continue;        }//全部花费都可以使用,将这个数字全输出一定最大         else        {            for(i=len-1;i>=0;i--)//位数外循环             {                for(j=9;j>=1;j--)//从大数开始                 {                    if(v-a[j]>=0&&(v-a[j])/min>=i)//剩下的颜料不能小于0                     {                        v-=a[j];                        printf("%d",j);                    }                }            }            printf("\n");        }    }    return 0;} 

思路:先考虑几种情况,首先先找出花费颜料数目最少的数字,
1:如果这个花费颜料最少的数字花费的颜料比总颜料大说明一个数字也刷不了直接输出-1.
2:当总颜料数除以花费颜料做少的数字的花费的油漆数能整除时,那么最优的情况一定是全部输出这个数字
3:当所有特殊情况都考虑过后,此时就是挨个选择了,用可以刷的字数作为外层循环次数,然后在内层按照从9到1(贪心策略)进行挨个选择,如果当前数字花费颜料a[j]<=v 且(v-a[j])/minx >= i.那么就把这个数字刷上,同时总颜料减去这个数字的花费。跳出内层循环。

0 0