南阳oj(nyoj) 791 Color the fence

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

思路:先考虑几种极端情况,将极端情况分离出去,极端情况如下,先找出花费油漆数目最少的数字,

1:如果这个花费油漆最少的数字花费的油漆比总油漆大说明一个数字也刷不了直接输出-1.

2:当总油漆数除以花费油漆做少的数字的花费的油漆数能整除时,那么最优的情况一定输出一堆都是这个数字的数字

3:当所有特殊情况都考虑过后,此时就是挨个选择了,这里用cnt=v/minx(最多能刷cnt份数字)作为外层循环次数

然后在内层按照从9到1(贪心策略)进行挨个选择,如果当前数字花费油a[j]<=v 且(v-a[j])/minx >= i.那么就把这个数字刷上,同时总油漆减去这个数字的花费。跳出内层循环。

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std ;

int a[10] ;
double b[10] ;
int v ;

int main()
{
    while(scanf("%d",&v)!=EOF)
    {
        int minx = 1e9 ;
        int k = 0 ;
        for(int i = 1 ;i<=9 ;i++)
        {
            scanf("%d",&a[i]) ;
            if(a[i] <= minx)
            {
                minx = a[i] ;
                k = i ;
            }
        }
        if(v < minx)    //最小花费的数字都没法画
        {
            printf("-1\n") ;
            continue ;
        }
        int cnt = v/minx ;
        for(int i = cnt-1 ;i>=0 ;i--)    //最多能画cnt个数字
        {
            for(int j = 9 ;j>=1 ;j--)
            {
                if(v-a[j]>=0 && (v-a[j])/minx >= i)
                {
                    v -= a[j] ;
                    printf("%d",j) ;
                    break ;
                }
            }
        }
        printf("\n") ;

    }
    return 0 ;
}
时间: 2024-12-28 21:05:18

南阳oj(nyoj) 791 Color the fence的相关文章

nyoj 791 Color the fence 【贪心】

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

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

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

南阳oj NYOJ 动物统计加强版 数据结构 题目290

 /*动物统计加强版 时间限制:3000 ms  |  内存限制:150000 KB 难度:4 描述 在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单.科学家想判断这片森林中哪种动物的数量最多,但是由于数据太过庞大,科学家终于忍受不了,想请聪明如你的ACMer来帮忙. 输入第一行输入动物名字的数量N(1<= N <= 4000000),接下来的N行输入N个字符串表示动物的名字(字符串的长度不超过10,字符串全为小写字母,并且只有一组测试数

【南阳OJ分类之语言入门】80题题目+AC代码汇总

声明: 题目部分皆为南阳OJ题目. 代码部分包含AC代码(可能不止一个)和最优代码,大部分都是本人写的,并且大部分为c代码和少部分c++代码and极少java代码,但基本都是c语言知识点,没有太多差别,可能代码有的写的比较丑,毕竟知识有限. 语言入门部分题基本都较为简单,是学习编程入门的很好练习,也是ACM的第一步,入门的最佳方法,望认真对待. 本文由csdn-jtahstu原创,转载请注明出处,欢迎志同道合的朋友一起交流学习.本人QQ:1373758426和csdn博客地址. now begi

codeforces349B - Color the Fence 贪心+DP

题意:1-9每个字母需要消耗ai升油漆,问你用油漆最多刻意画多大的数字 解题思路: 首先我们要贪心,可以知道我最优是先使我们位数尽可能长,然后才是就是位数长的情况下使得从最高位开始尽可能大.所以要取满足最长位最小的那个数,方便我们DP 解题代码: 1 // File Name: 349b.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月24日 星期四 21时40分13秒 4 5 #include<vector> 6 #include&

349B - Color the Fence

Color the Fence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description 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.

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1

【南阳OJ分类之大数问题】题目+AC代码汇总

声明:题目部分皆为南阳OJ题目,代码部分包含AC代码(可能不止一个)和标程. 由于大数问题用c/c++写比较麻烦,而Java的大数类又很好用,所以基本为java代码.实际上竞赛很少会考大数问题,因为竞赛是比的算法,而不是语言特性,不过很多都是大数据,数据上千万级别的,所以算法又很关键,显然那和这篇博客也没啥关系. 题目不是太难,大家和本人就权当学习或复习下Java吧O(∩_∩)O~. 该分类南阳oj地址:http://acm.nyist.edu.cn/JudgeOnline/problemset

nyoj 题目10 skiing —— 南阳oj

题目信息例如以下: skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区域必须向下倾斜,并且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出. 数组的每一个数字代表点的高度. 以下是一个样例 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13