解题思路:使用数组c来存储从i到n最小的b[i],那么要使字典最大化,a[1]=k/c[1]是一定成立的,如果c[i]=0,说明从该i加c[i-1]次1能使字典最大化。
AC代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const long long INF = 1e9 + 10;
const int maxn = 2e5 + 10;
long long a[maxn], b[maxn], c[maxn];
void Init()
{
memset(a, INF, sizeof a);
memset(c, INF, sizeof c);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
Init();
int n;
long long k;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &b[i]);
}
scanf("%lld", &k);
c[n] = b[n];
for (int i = n - 1; i > 0; i--)
{
c[i] = min(b[i], c[i + 1]);//表示从i到n的最小的b值
}
for (int i = n; i >= 2; i--)
{
c[i] -= c[i - 1];
}
for (int i = 1; i <= n; i++)
{
if (c[i])
{
a[i] = min(k / c[i], a[i - 1]);
if (k / c[i] < a[i - 1])
{
k %= c[i];
}
else
{
k -= a[i - 1] * c[i];
}
}
else
{
a[i] = a[i - 1];
}
printf("%lld", a[i]);
if (i != n)
{
printf(" ");
}
}
printf("\n");
}
}