Nyoj 791--color The fence —————— "greedy"

Source: Internet
Author: User

Color The fence time limit:MS | Memory limit:65535 KB Difficulty:2
Describe

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 are, 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.

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

Input
There is multiple test cases.
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).
Output
Printf The maximum number Tom can write on the fence. If He has too little paint for any digit, print-1.
Sample input
55 4 3 2 1 2 3 4 529 11 1 12 5 8 9 10 6
Sample output
5555533


Test instructions: give you a V-liter paint, give the number of words 1--9 the corresponding paint cost, so that you use so much paint to draw the largest numbers.

Problem solving ideas;

/*
The greedy thought of this question is quite obvious. We want to make the number as large as possible, then we must ensure that the number of the place to be as long as possible
As long as possible on the basis, let the high number as large as possible, this is our greedy strategy. We know that by dividing the smallest number, you can
The number of giving way is the longest, but the longest can be obtained not only by the smallest number. such as: V=5 a={2,3,24,32,31,14,15,7,9},
Although the maximum length can be obtained is 2, and by dividing by 2 can result in 11, but the best result is 21. So as long as we can
It is guaranteed that the high-order number we get is larger and the number of digits is equal than the minimum number chosen.
*/

#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\n");            Continue;        }        for (i=v/minv;i>=1;i--) {//Current dye If dye minimum cost that number can be dyed how many bits for            (j=9;j>=1;j--) {  //from large to small traverse the number                if (v>= digit[j]&& (V-digit[j])/minv+1>=i) {                        //The current dye can dye a larger number and guarantee the same number of digits that can be dyed with the least amount of dye                        //greedy selection of the number                    printf ("%d", j);                    V-=DIGIT[J];}}        } printf ("\ n");    }    return 0;}

  



Nyoj 791--color The fence —————— "greedy"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.