Codeforces D. Color the Fence(贪心)

题目描述:

D. Color the Fence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

Input

The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9(1 ≤ ai ≤ 105).

Output

Print 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.

Examples
Input
5
5 4 3 2 1 2 3 4 5
Output
55555
Input
2
9 11 1 12 5 8 9 10 6
Output
33
Input
0
1 1 1 1 1 1 1 1 1
Output
-1

思路:

题目意思是给9个数字的价钱,和现在拥有的钱,看能不能买,能的话把能买到数字凑成最大的数是多少。

刚开始,想得简单了(只想法),就知道位数越多越好,那我就选最便宜的买咯,全买最便宜的,也想复杂了,还创建结构体,在sort写了个cmp结构体排序,输入的时候就直接忽略钱不够的数字,从钱最少的且数值最大的开始。(该简单的时候不简单,该复杂的时候又想简单了ε=(´ο`*))))

结果这个策略不行,为啥,有没有可能我的钱花不完,然后把剩下的钱买够得到的,尽可能多的情况下数值又尽量大的颜料。emmm,有道理,但是既然剩下的钱都可以买比现在的更便宜的颜料,那我直接全部买这种便宜的颜料数量上岂不更多?所以是不可能的啦。(一开始就这个思路还写了个递归,最后就像写bug一样把自己绕进去了QAQ)

那么,我要是不用全部买最便宜的颜料后剩下的钱呢?

什么意思,就是我假设可以买n个最便宜的颜料,嗯,那我买n-1个试试,剩下的钱看能不能买个9?买两个9?两个不行,那我能不能再买个8,两个8?,...,这样从大到小一直下去,就保证了数字位数最大,而且数值最大。代码对我来说有一点tricky。

知识点:贪心

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <memory.h>
 4 #define INF 0x3f3f3f3f
 5 using namespace std;
 6 int n;
 7 int pri[10];
 8 int minm;
 9 int main()
10 {
11     cin >> n;
12     int flag = 0;
13     minm = INF;
14     for(int i = 1;i<10;i++)
15     {
16         cin >> pri[i];
17         if(n>pri[i])
18         {
19             flag = 1;
20         }
21         if(pri[i]<minm)
22         {
23             minm = pri[i];
24         }
25     }
26     if(flag==0)
27     {
28         cout << -1 << endl;
29     }
30     else
31     {
32         int sum = n/minm;
33         for(int i = sum;i>0;i--)
34         {
35             for(int j = 9;j>0;j--)
36             {
37                 if(n>=pri[j]&&(n-pri[j])/minm==i-1)//可以买j,而且剩下的钱还可以买i-1个数
38                 {
39                     n -= pri[j];
40                     cout << j;
41                 }
42             }
43         }
44         cout << endl;
45     }
46     return 0;
47 }

 

转载于:https://www.cnblogs.com/zhanhonhao/p/11248776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值