NYOJ-791 Color the fence (贪心)
Color the fence
- 描述
-
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=5,array={2,3,24,32,31,14,15,7,9},
虽然可以得到最长长度是2,且由除以2可得到结果11,但是最优的结果却是21。所以只要我们能
保证我们得到的高位数字比选择最小的数所得高位数字大并且位数相等即可。 AC代码:#include <stdio.h> int main(void)
{
int V;
while (~scanf("%d", &V))
{
int array[],i;
int min = ;
for (i = ; i <= ; i++)
{
scanf("%d", &array[i]);
if (array[i] <= min)
{
min = array[i];
}
} if (min > V)
{
printf("-1\n");
continue;
}
int bit = V / min;
while(bit--)
{
for (int j = ; j >= ; j--)
{
if (V >= array[j] && (V - array[j] )/ min == bit)
{
printf("%d", j);
V -= array[j];
break;
}
}
}
putchar('\n');
}
return ;
}
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 w ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 349B - Color the Fence
349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...
- Codeforces D. Color the Fence(贪心)
题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- nyoj Color the fence
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- ACM Color the fence
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- 349B - Color the Fence
Color the Fence Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- 【贪心】Codeforces 349B.Color the Fence题解
题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
随机推荐
- RedHat9.0下载地址
RedHat下载:http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/
- NVIC
1中断:每一个中断都会对应一个服务程序 2NVIC 他的做用是负责中断优先级管理 3IP bit[7:4]每一个中断都有一个IP寄存器,用于设置中断相关的属性 AIRCR[10:8]只一个AIRCR寄 ...
- 2016 Vultr VPS最新优惠码,赠送新用户70美元,亲测有效
vultr肯定疯了,从来没有哪家海外vps像vultr那么大力度的优惠.近期,vultr vps再度推出优惠码,任何新用户注册即送20美元!要知道,vultr vps最便宜的vps套餐只要5美元/月, ...
- Java多线程和死锁
一 .多线程: 售票窗口简单实例: public class SaleTicket { public static class Sale implements Runnable{ ; // @Over ...
- Oralce生成前N年的年数据
今天做一个统计报表的时候正好碰到这个问题,原来,一般是通过后台代码来生成.现在直接通过oracle来生成,记录一下. 方法一: SELECT YEAR FROM ( , UNION SELECT TO ...
- 兼容 console 没删除引起 低级浏览器 报错问题
/*重写connsole.log,防止调试代码出错*/if (!window.console){ var names = ["log", "debug", &q ...
- Fighting For 2017 Season Contest 1
比赛地址[https://vjudge.net/contest/147011#problem/A].960626 题目一:[http://codeforces.com/problemset/probl ...
- java 中递归的实现 以及利用递归方法实现汉诺塔
今天说下java语言中比较常见的一种方法,递归方法. 递归的定义 简单来说递归的方法就是"自己调用自己",通过递归方法往往可以将一个大问题简单化,最终压缩到一个易于处理的程度.对于 ...
- clone()方法、深复制和浅复制
clone方法 Java中没有明确提供指针的概念和用法,而实质上没个new语句返回的都是一个指针的引用,只不过在大部分情况下开发人员不需要关心如何去操作这个指针而已. 在实际编程中,经常会遇到从某个已 ...
- EasyUI 日期选择插件封装成选择到月份的插件
将普通的日期选择插件封装成选择到月份的插件: var nowMonth = new Date(); var month = ...