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 ...
随机推荐
- 双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)
题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时g ...
- # Tallest Cows(差分)
Tallest Cows(差分) n头牛,给出最高牛的位置和身高,其他牛身高未知,给出m对相对关系,表示可以相互看见当且仅当他们中间的牛都比他们矮.求每头牛身高最大值是多少. 差分数组的性质:前缀和为 ...
- xargs、chattr命令
一.xargs:将标准输入转化成命令行参数 用法:xargs [OPTION] ... COMMAND INITIAL-ARGS ...使用参数INITIAL-ARGS运行COMMAND,并从输入中读 ...
- Dapper多表查询时子表字段为空
最近在学习使用Dapper时百度了一篇详细使用教程,在做到多表查询的时候,出现如下情况. 使用的SQL如下, SELECT * FROM [Student] AS A INNER JOIN [Juni ...
- O024、Nova组件如何协同工作
参考https://www.cnblogs.com/CloudMan6/p/5415836.html Nova 物理部署方案 前面大家已经看到 Nova 由很多子服务组成,我们也知道OpenS ...
- centos安装配置php
PHP的安装同样需要经过环境检查.编译和安装3个步骤. 1.首先用百度搜索 “PHP:Downloads”, 点击第一个网页: 选择5.5.37版本,选择 .tar.gz 格式的文件: 来到镜像列表网 ...
- API接口之安全篇
APP.前后端分离项目都采用API接口形式与服务器进行数据通信,传输的数据被偷窥.被抓包.被伪造时有发生,那么如何设计一套比较安全的API接口方案呢? 一般的解决方案如下: 1.Token授权认证,防 ...
- JS中的事件传播流程
JS中的事件传播流程 1,Javascript与HTML之间的交互是通过事件实现的. 事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间. 可以使用侦听器来预定事件,以便事件发生时执行相应代码. 2 ...
- openlayers之天地图为底图 叠加其他底图 加载遇到的各种报错
今天以前使用天地图为底图时,map里source的定义:http://t3.tianditu.com~~~ 今天突然报跨域的错误,原有地址访问受限,可是key值是有的 最后发现就是前半段的原因 将前半 ...
- Halide安装指南release版本
Halide安装指南 本版本针对Halide release版本 by jourluohua 使用的是Ubuntu 16.04 64位系统默认Gcc 4.6 由于halide中需要C++ 11中的特性 ...