CodeForces 349B - Color the Fence

Igor想要在Tanya家对面的围栏上写下一个最大的数字来表达爱意,但受到可用油漆量的限制。本篇介绍了一种算法,通过贪心策略帮助Igor找到能写的最大数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Igor has fallen in love with Tanya. Now Igor wants to show his feelingsand write a number on the fence opposite to Tanya's house. Igor thinks that thelarger 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 ofpaint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igorwon'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). Thesecond 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 toolittle paint for any digit (so, he cannot write anything), print -1.

Sample test(s)

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

 

 

思路:

贪心

将数字1~9按ai从小到大排序,ai一样的大的排在前面

然后用v除以最小的ai,确定输出数字的长度

然后从最高位到最低位判断能否替换成尽量大的数


程序:
#define _CRT_SECURE_NO_WARNINGS

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

#define L(u) (u<<1)
#define R(u) (u<<1|1)
#define lowbit(x) (x&-x)
#define rep(i,x,y) for (i=x;i<=y;i++)
#define ll __int64
#define max(x,y) ((x>y)?(x),(y))
#define min(x,y) ((x<y)?(x),(y))
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define slld(x) scanf("%lld",&x)

const int N = 1000005;

struct node
{
	int v, id;
}a[12];

bool cmp(node a, node b)
{
	if (a.v == b.v)
		return a.id > b.id;
	else
		return a.v<b.v;
}

bool cmp2(node a, node b)
{
	return a.id<b.id;
}

int main()
{
	int v;
	int l = 0, i;
	while (sd(v) != EOF)
	{
		rep(i, 1, 9)
		{
			sd(a[i].v);
			a[i].id = i;
		}

		sort(a + 1, a + 10, cmp);

		int temp = a[1].id;
		l = v / a[1].v;
		if (l == 0)
		{
			printf("-1\n");
			return 0;
		}
		v -= a[1].v*l;

		sort(a + 1, a + 10, cmp2);

		rep(i, 1, l)
		{
			int j, ans = temp;
			for (j = 9; j > temp; j--)
			{
				if (a[j].v - a[temp].v <= v)
				{
					ans = j;
					v -= a[j].v - a[temp].v;
					break;
				}
			}
			printf("%d", ans);
		}
		printf("\n");

	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值