题目描述:

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。

知识点:贪心

代码:

 #include <iostream>
#include <algorithm>
#include <memory.h>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int pri[];
int minm;
int main()
{
cin >> n;
int flag = ;
minm = INF;
for(int i = ;i<;i++)
{
cin >> pri[i];
if(n>pri[i])
{
flag = ;
}
if(pri[i]<minm)
{
minm = pri[i];
}
}
if(flag==)
{
cout << - << endl;
}
else
{
int sum = n/minm;
for(int i = sum;i>;i--)
{
for(int j = ;j>;j--)
{
if(n>=pri[j]&&(n-pri[j])/minm==i-)//可以买j,而且剩下的钱还可以买i-1个数
{
n -= pri[j];
cout << j;
}
}
}
cout << endl;
}
return ;
}

Codeforces D. Color the Fence(贪心)的更多相关文章

  1. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  2. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  3. 【贪心】Codeforces 349B.Color the Fence题解

    题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...

  4. codeforces B. Color the Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...

  5. CodeForces 349B Color the Fence (DP)

    题意:给出1~9数字对应的费用以及一定的费用,让你输出所选的数字所能组合出的最大的数值. 析:DP,和01背包差不多的,dp[i] 表示费用最大为 i 时,最多多少位,然后再用两个数组,一个记录路径, ...

  6. NYOJ-791 Color the fence (贪心)

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  7. nyoj 791——Color the fence——————【贪心】

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  8. ACM Color the fence

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  9. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

随机推荐

  1. haproxy2.0入门部署教程

    测试后发现,haproxy2.0和之前的版本部署有些许差异,配置文件的写法也是不同的 测试环境:Centos7.3 IP:172.16.1.227 172.16.1.228 部署httpd,页面内容为 ...

  2. 【Python学习之一】Python安装、IDE安装配置

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python2.X python3.X 所谓安装Python,安装的是 ...

  3. Bat批处理之for/f详解

    转自:https://www.cnblogs.com/zhangq/p/3988697.html 含有/F的for格式: FOR /F ["options"] %%i IN (fi ...

  4. LeetCode 976. 三角形的最大周长(Largest Perimeter Triangle) 33

    976. 三角形的最大周长 976. Largest Perimeter Triangle 题目描述 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. ...

  5. Java开发笔记(一百一十六)采用UDP协议的Socket通信

    前面介绍了如何通过Socket接口传输文本与文件,在示例代码中,Socket客户端得先调用connect方法连接服务端,确认双方成功连上后才能继续运行后面的代码,这种确认机制确保客户端与服务端的的确确 ...

  6. 不是所有OutOfMemoryError异常都跟内存有关

    一个老鸟遇到一个稀奇的问题后,如果只是想想,那么可能会失去一次丰富自己的机会. 如果从开始养成一个习惯,把所有难解决的问题都记录下来,面试的时候,也可能是给自己一次机会 *************** ...

  7. 跨域访问MVC

    using MvcApp.Filters; using System; using System.Collections.Generic; using System.Linq; using Syste ...

  8. Java对list进行分页,subList()方法实现分页

    /** * 自定义List分页工具 * @author hanwl */ public class PageUtil { /** * 开始分页 * @param list * @param pageN ...

  9. sql语句将图片插入image类型的字段中

    update table set photo=(SELECT * FROM OPENROWSET(BULK N'D:\no.png', SINGLE_BLOB) as Photo)    From:h ...

  10. python3基础之“函数(2)”

    1.def:定义一个函数 def f(x): return x+1 #返回函数值 a=f(2) print(a) >>3 def even_odd(x): if x%2==0: " ...