【CodeForces - 349B】Color the Fence (贪心,填数)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83717253

题干:

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

题目大意:

在墙上用一定量的油漆涂数字,尽量使涂出来的数字大。输入v是我拥有的油漆量,然后下一行九个数字告诉你涂每个数字所需要的油漆量。

解题报告:

   这题显然是个贪心,,问题就在于贪心正确性的证明。这题在青理工邀请赛的时候有过一个类似的题目也是贪心填数字。也是用类似的思想。

  首先用最小值来判断位数,那么答案肯定是这个位数的,那么我们来看在位数确定的情况下有没有可能改变最优解。我们最开始的写法也是可以改变最优解的,那就是把最后一位去掉然后看这一位可不可以填入别的数字,填的时候要满足后面的用最小值还可以填上。。然后就贪心就行了,,注意要判断a[j] > n 啊,不然后面那个if不就成负数了(其实这样写是无所谓的但是如果把minn除过来这么写的话就会出问题,比如-2/5本来是-(0.4)应该向下取整成-1但是因为优先级的问题,就成0了,,于是可能就把不能算上的情况算上了。因为你之所以可以直接用除法取整就是因为除法是默认向下取整啊但是在负数中就不是向下取整了所以就会出现很多问题,,所以保险起见还是少用除法多用加减乘、、、)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e6 + 5;
int a[22],ans[MAX];
int main()
{
	int n,minn=0x3f3f3f3f,mini,flag = 1;
	cin>>n;
	for(int i = 1; i<=9; i++) {
		scanf("%d",a+i);
		if(a[i] <= minn) {
			minn = a[i];
			mini = i;
		}
	}
	if(n < minn) {
		puts("-1");return 0 ;
	}
	int zxz = n/minn;//一共就  这么多位
	for(int i = zxz; i>=1; i--) {
		for(int j = 9; j>=1; j--) {
			if(a[j] > n) continue;
			if( n-a[j] < (i-1)*minn) continue;
			ans[i] = j;
			n-=a[j];
			break;
		}
	} 
	for(int i = zxz; i>=1; i--) printf("%d",ans[i]);
	return 0 ;
 }

附上wa的代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int bk[500];
int a[22];
int main()
{
	int n,minn=0x3f3f3f3f,mini,flag = 1;
	cin>>n;
	for(int i = 1; i<=9; i++) {
		scanf("%d",a+i);
		if(a[i] <= minn) {
			minn = a[i];
			mini = i;
		}
	}
	if(n < minn) {
		puts("-1");return 0 ;
	}
	int zxz = n/minn;
	if(n%minn == 0) {
		for(int i = 1; i<=zxz; i++) putchar(mini+'0');
		return 0 ;
	}
	zxz--;
	int res = n-zxz*minn,resi;
	for(int i = 1; i<=9; i++) {
		if(a[i] <= res) {
			resi = i;
		}
	}
	for(int i = 1; i<=zxz; i++) putchar(mini + '0');
	putchar(resi+'0');
	return 0 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83717253