CodeForces 349B--Color the Fence(贪心)
B. Color the Fence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has.
Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.
Help Igor find the maximum number he can write on the fence.
Input
The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105).
Output
Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.
Examples
Input
5
5 4 3 2 1 2 3 4 5
Output
55555
Input
2
9 11 1 12 5 8 9 10 6
Output
33
Input
0
1 1 1 1 1 1 1 1 1
Output
-1
思路:
位数越多当然数越大,首先要考虑的是每一位都一样位数最大的情况。
但若每一位都是一样,可能会有剩余的油漆。
所以我们要找到在此长度的情况下,数值的最大值
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int main()
{
int a[10], sum, minn, k, i, j, cnt, r;
while (~scanf("%d", &sum))
{
minn = 1000000005;
k = 0;
for (i = 1; i <= 9; i++)
{
scanf("%d", &a[i]);
if (a[i] < minn) //找出花费最小的颜料数
{
minn = a[i];
k = i;
}
else if (a[i] == minn && k < i) //花费与最少的相等,自然取数字大的
k = i;
}
if (sum < minn) //最少的花费都大于总颜料,自然不行
{
printf("-1\n");
continue;
}
cnt = sum / minn; //最小花费能得到的数字长度
r = sum % minn;
if (!r) //若总颜料能整出最小花费,全部输出这个数一定是最大
{
for (i = 1; i <= cnt; i++)
printf("%d", k);
printf("\n");
continue;
}
while (sum > 0) //总颜料还没用完,找出与最小花费长度相等的最大数
{
int x = 0;
for (i = 1; i <= 9; i++)
{
int s = sum - a[i]; //减去这个数字的花费
if (s < 0) //这个数字会使颜料用完,换下一个颜料
continue;
if (s / minn == cnt - 1 && x < i) //减去该数字的花费之后,剩下的除以最小的长度是原来长度-1,必定是最符合的,在所有最符合的状况中找到最大的
x = i;
}
if (x)
{
sum -= a[x]; //放了一个数字,总花费减少
cnt--;//长度减一
printf("%d", x); //输出这个数字
}
}
printf("\n");
} return 0;
}
CodeForces 349B--Color the Fence(贪心)的更多相关文章
- 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 349B.Color the Fence题解
题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...
- CodeForces 349B Color the Fence (DP)
题意:给出1~9数字对应的费用以及一定的费用,让你输出所选的数字所能组合出的最大的数值. 析:DP,和01背包差不多的,dp[i] 表示费用最大为 i 时,最多多少位,然后再用两个数组,一个记录路径, ...
- Codeforces D. Color the Fence(贪心)
题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 349B - Color the Fence
Color the Fence Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
- NYOJ-791 Color the fence (贪心)
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- nyoj 791——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 ...
随机推荐
- CF 1133C Balanced Team
题目链接:http://codeforces.com/problemset/problem/1133/C 题目分析 (个人感受:我看错了题目,硬是写了近一个小时!) 这个题目要求一个最长的序列,使得这 ...
- XPath读取xml文件
1.创建解析工厂 2.创建解析器 3.读xml文件,生成w3c.docment对象树 4.创建XPath对象 5.通过路径查找对象 例子: import javax.xml.parsers.Docum ...
- 2019中山纪念中学夏令营-Day1[JZOJ]
T1 题目描述: 1999. Wexley接苹果(apple) (File IO): input:apple.in output:apple.out 时间限制: 1000 ms 空间限制: 1280 ...
- spark教程(九)-操作数据库
数据库也是 spark 数据源创建 df 的一种方式,因为比较重要,所以单独算一节. 本文以 postgres 为例 安装 JDBC 首先需要 安装 postgres 的客户端驱动,即 JDBC 驱动 ...
- springboot2.X版本得@Transactional注解事务不回滚不起作用
参考文章 https://my.oschina.net/happyBKs/blog/1624482 https://blog.csdn.net/u011410529/article/detail ...
- C++ 数组操作符重载、函数对象分析、赋值操作符
string类型访问单个字符 #include <iostream> #include <string> #include <sstream> using name ...
- 导出excel模版
方法一: public void ToExcel(){ //第一步:获取模版物理路径 string file_1 = Server.MapPath("/Content/Excel/downE ...
- PHP高级进阶之路
一:常见模式与框架 学习PHP技术体系,设计模式,流行的框架 常见的设计模式,编码必备 Laravel.ThinkPHP开发必不可少的最新框架 YII.Symfony4.1核心源码剖析 二:微服务架构 ...
- 浅尝https
HTTPS http超文本传输协议,所以的东西都是明文传输,容易被拦截,被攻击,我们希望能对通话内容进行加密,那么因此而生,出现了https https:在http的基础上新增加了SSL层 先放图 / ...
- Linux下make cmake 编译等啥意思?
写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执行文件,如.exe. 但如果源文件太多,一个一个编译时就会特别麻烦 ...