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. 「TJOI2013」攻击装置

    题目链接 戳我 \(solution\) 这道题和网络24题之骑士共存问题很相似 只是输入方式不一样而已 详细见:这儿 \(Code\) #include<bits/stdc++.h> # ...

  2. Linux安装Oracle调整tmpfs以突破1.7G的限制

    调整/dev/shm的大小 ---------------------------------------------------------1.查看大小 df -h /dev/shm [@more@ ...

  3. DHCP与PPPOE 区别

    1.静态IP的方式,如果是占用一个INTERNET的IP的话,上网都是很贵的,当然这个也是最方便的,开机就能上网,不用做任何拨号或者认证的过程.2.PPPOE,只是多了一个获得IP的过程,一旦获得了I ...

  4. [转]Why you shouldn't use set (and what you should use instead)

    Why you shouldn't use set (and what you should use instead) --- stl::set和sorted ector对比Matt Austern ...

  5. 浅谈C#中的委托、事件与异步

    从刚接触c#编程到现在,差不多快有一年的时间了.在学习过程中,有很多地方始终似是而非,直到最近才弄明白. 本文将先介绍用法,后评断功能. 一.委托 基本用法: 1.声明一个委托类型.委托就像是‘类'一 ...

  6. Java 实现邮件的发送

                                             Java 实现邮件的发送 开发邮箱发送功能必须看邮箱方面的资料 改一些东西  (我的是qq 邮箱哟   开通 POP3 ...

  7. liunx 安装 phpstudy

    phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 phpStudy for Linux 支持Apache/Ng ...

  8. POJ_2010 Moo University - Financial Aid 【堆预处理】

    一.题面 POJ2010 二.分析 堆预处理 首先可以考虑吧随便取一个点,判断两侧的最小的总费用是多少,然后相加判断是否满足条件.如果直接判断会超时,所以需要用大根堆预处理一下.先看从分数最小的往最大 ...

  9. UVA - 11552 DP 划分

    每k个字符划分一个组,该组内字符顺序可以任意重排,定义块为最长的连续的字符子串,求长度为m*k的字符串中最少的块的数目 设\(dp[i][j]\):前\(i\)组中第\(i\)组结尾为\(j\)的最优 ...

  10. 1129 Recommendation System (25 分)

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...