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 题意:给你一个v升油漆,给出数字1--9对应的油漆花费,让你用那么多油漆画出最大的数。 解题思路;

/*
这题的贪心思想比较明显。我们要让能得到数尽量大,那么就要保证让位数尽量长,在位数
尽量长的基础上,让高位数尽量大,这是我们的贪心策略。我们知道,除以最小的数,可以
让位数最长,然而最长可以不仅仅是由最小的数得到。如:v=5 a={2,3,24,32,31,14,15,7,9},
虽然可以得到最长长度是2,且由除以2可得到结果11,但是最优的结果却是21。所以只要我们能
保证我们得到的高位数字比选择最小的数所得高位数字大并且位数相等即可。
*/

#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--){//目前的染料如果染最小花费那个数字可以染多少位
for(j=9;j>=1;j--){ //从大到小遍历数字
if(v>=digit[j]&&(v-digit[j])/minv+1>=i){
//目前的染料能染较大的数字且保证跟染最小那个数字可染的位数相同
//贪心选择染大的数字
printf("%d",j);
v-=digit[j];
}
}
}printf("\n");
}
return 0;
}

  


nyoj 791——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. NYOJ-791 Color the fence (贪心)

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

  4. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. nyoj Color the fence

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

  6. ACM Color the fence

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

  7. 349B - Color the Fence

    Color the Fence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

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

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

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

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

随机推荐

  1. Windows下查看进程状态/信息

    任务管理器 tasklist pslist(https://docs.microsoft.com/en-us/sysinternals/downloads/pslist) Process Explor ...

  2. javascript ie8兼容 a标签href javascript:void(0);

    ie8兼容 a标签href javascript:void(0); 尽量不要用javascript:协议做为A的href属性,这样不仅会导致不必要的触发window.onbeforeunload事件;

  3. python 中如何判断list中是否包含某个元素

    在python中可以通过in和not in关键字来判读一个list中是否包含一个元素 theList = ['a','b','c'] if 'a' in theList: print 'a in th ...

  4. CKEditor编辑器的使用方法

    CKEditor  网页中实现所见即所得的编辑器. 使用步骤: 1.下载CKEditor  下载地址:http://ckeditor.com/download 2.添加CKEditor的文件夹到项目中 ...

  5. SpringMVC返回JSON数据

    1.导入json的jar包2.在Controller类中添加 //查看用户信息 ?json //params="json"的意思是访问view这个方法的时候,必须有一个参数json ...

  6. flink学习笔记-split & select(拆分流)

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  7. 冒泡排序 思想 JAVA实现

    已知一个数组78.75.91.36.72.94.43.64.93.46,使用冒泡排序将此数组有序. 冒泡排序是一个运行时间为O(N²)的排序算法. 算法思想:(已从小到大为例) 78.75.91.36 ...

  8. css grid学习笔记

    仅为自己用而收藏罢了 w3c官方文档 https://www.w3.org/TR/css-grid-1/#grid-items w3cplus(zhongwenban ) 大漠博主的系列文章 基础知识 ...

  9. Java过滤器详细文档,简介,实例,应用

    简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...

  10. 老男孩python作业5-开发一个简单的python计算器

    开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...