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个数字,现 ...
随机推荐
- 3.struts2访问Servlet API,并和mybaits实现全套增删改查
1.创建数据库脚本userinfo.sql prompt PL/SQL Developer import file prompt Created on 2016年5月19日 by pc set fee ...
- CentOS 7中将Tomcat设置为系统服务
tomcat 需要增加一个pid文件,在tomca/bin 目录下面,增加 setenv.sh 配置,catalina.sh启动的时候会调用,在该文件中添加如下内容 CATALINA_PID=&quo ...
- linode开通Paypal付款方式
vps服务器品牌linode近期新闻不断.今天是linode成立13周年,全部套餐免费升级翻倍内存,所以现在linode最低配置套餐内存是2GB,每月2TB流量,40Gb机房带宽,非常超值. 长期以来 ...
- jvm原理及调优
一.java内存管理及垃圾回收 jvm内存组成结构 jvm栈由堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: (1)堆 所有通过new创建的对象的内存都在堆中分配,堆的大小可以通过-Xmx和- ...
- 用 monitorix 开启linux图形化监控
# yum install monitorix # service monitorix starthttp://host:8080/monitorix/
- VS发布网站步骤(先在vs上发布网站到新的文件夹,然后挂到iis上面)
VS发布网站步骤(先在vs上发布网站到新的文件夹,然后挂到iis上面) 首先用vs2010打开一个Asp.Net项目, 也可以通过vs菜单->生成->发布网站 选择发布网站的路径 ...
- 6、iOS快速枚举
今天在写程序的时候想在当前视图跳转的时候释放掉当前视图上面add的一些子视图.因为add的子视图有些是在别的类里面add进来的,当前页面不知道自己当前有哪几个类型的子视图.这样,我就想到了用循环遍历来 ...
- 李明杰的视频和李明杰的博客是学习OC的基础
http://www.cocoachina.com/bbs/read.php?tid=196664
- Git 添加自己分支 pull request
1.找到项目地址 这里,我们可以找到项目地址,比如:https://github.com/*****/Cplusplus_Thread_Lib,然后点击页面右上角的 "fork" ...
- echart+jquery+json统计TP数据
由于工作需要,需要统计交易数据的TP50,TP90,TP95,TP99.采用的前端技术是jquery+json+echart. 一.TP定义(谷歌) Calculating TP is very si ...