[CF349B] Color the Fence - 贪心
Description
写每个数字会花费一定油漆 ai,没有数字 0,只有 v 升油漆,问能写出的最大数字是多少。
Solution
首先我们肯定会最大化数字的长度
然后我们会从前到后依次最大化数字,即试图让第一个数字取最大,然后……
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000005;
int a[N], s[N], v;
signed main()
{
ios::sync_with_stdio(false);
cin >> v;
for (int i = 1; i <= 9; i++)
cin >> a[i];
int min_value = 1e9, min_id = 0;
for (int i = 1; i <= 9; i++)
{
if (a[i] <= min_value)
{
min_value = a[i];
min_id = i;
}
}
int len = 0;
while (v >= min_value)
{
s[++len] = min_id;
v -= min_value;
}
for (int i = 1; i <= len; i++)
{
int max_id = 0;
for (int j = min_id + 1; j <= 9; j++)
{
if (a[j] > min_value && a[j] - min_value <= v)
max_id = j;
}
if (max_id)
{
v -= a[max_id] - min_value;
s[i] = max_id;
}
cout << s[i];
}
if (len == 0)
cout << -1;
}