Codeforces Round #202 (Div. 2) B. Color the Fence
B. Color the Fencetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has.
Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.
Help Igor find the maximum number he can write on the fence.
InputThe first line contains a positive integer v (0?≤?v?≤?106). The second line contains nine positive integers a1,?a2,?...,?a9 (1?≤?ai?≤?105).
OutputPrint the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.
Sample test(s)input55 4 3 2 1 2 3 4 5output
55555input
29 11 1 12 5 8 9 10 6output
33input
01 1 1 1 1 1 1 1 1output
-1
先凑位数多的,再凑首个数字大的,就可以了!
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;#define M 100050struct node{ int p,i;}pri[14];bool cmp(node a,node b){ if(a.p!=b.p)return a.p<b.p; else return a.i>b.i;}int main(){ int v,i; while(scanf("%d",&v)!=EOF){ for(i=1;i<=9;i++) scanf("%d",&pri[i].p),pri[i].i=i; sort(pri+1,pri+10,cmp); int n=v/pri[1].p; if(n==0)printf("-1\n"); else { int res=v-n*pri[1].p,maxx=pri[1].i,kk=0; while(1){ int temp=-1;maxx=pri[1].i; for(i=1;i<=9;i++){ if(res+pri[1].p>=pri[i].p&&maxx<pri[i].i){ temp=i; maxx=pri[i].i; } } if(temp==-1)break; res=res-pri[temp].p+pri[1].p; printf("%d",maxx);kk++; } for(;kk<n;kk++)printf("%d",pri[1].i); printf("\n"); } } return 0;}