D. Prefix Purchase

D. Prefix Purchase

You have an array a of size n, initially filled with zeros (a1=a2==an=0). You also have an array of integers c of size n.

Initially, you have k coins. By paying ci coins, you can add 1 to all elements of the array a from the first to the i-th element (aj+=1 for all 1ji). You can buy any ci any number of times. A purchase is only possible if kci, meaning that at any moment k0 must hold true.

Find the lexicographically largest array a that can be obtained.

An array a is lexicographically smaller than an array b of the same length if and only if in the first position where a and b differ, the element in array a is smaller than the corresponding element in b.

Input

The first line contains a single integer t (1t104) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains a single integer n (1n2105) — the size of arrays a and c.

The second line of each test case contains n integers c1,c2,,cn (1ci109) — the array c.

The third line of each test case contains a single integer k (1k109) — the number of coins you have.

It is guaranteed that the sum of all n values across all test cases does not exceed 2105.

Output

For each test case, output n integers a1,a2,,an — the lexicographically largest array a that can be obtained.

Example

input

4
3
1 2 3
5
2
3 4
7
3
3 2 1
2
6
10 6 4 6 3 4
7

output

5 0 0 
2 1 
2 2 2 
2 2 2 2 2 1 

Note

In the first test case, a1 cannot be greater than 5, and if we buy c1 five times, we will run out of money, so a=[5,0,0].

In the second test case, a1 cannot be greater than 2, but we can buy c1 and c2 once each (buying c2 twice is not possible), so a=[2,1].

 

解题思路

  如果有下标 i<j,并且 cicj,那么很明显买入 cj 要比 ci 更优,因为不仅前缀的长度 ji 大,而且价格也不超过 ci。因此要把所有这样的 ci 删掉,有点类似于单调栈,最后栈内的元素(存的是下标)是严格递增的,满足 i<j,则 ci<cj

  然后就是贪心地选择栈内的哪些元素购买,使得序列 a 的字典序最大,这步卡了我很久。设栈的大小为 sz,栈底元素是 stk[1],由于栈底元素对应的 cstk[1] 最小,因此要使得 a 的前 stk[1] 个元素字典序最大,那么在下标 stk[1] 处最多能买入 kcstk[1] 次。对于前 stk[1] 个元素而言这是上界,否则如果在其他位置 stk[i],i[2,sz] 买入,那么字典序肯定不如这个上界大,因为 cstk[i]>cstk[1],kcstk[i]<kcstk[1]

  买入后会有剩余,记作 k=kmodcstk[1]在保持 a 的前 stk[1] 个元素字典序最大的前提下,看看能不能买入其他的 stk[i] 来替换 stk[1],使得字典序更大。对于 stk[2] 的话,如果想要用 stk[1] 替换,那么需要 cstk[2]cstk[1] 的花费,而最多可以替换 kcstk[2]cstk[1] 次。另外还要考虑到在 stk[1] 买入的次数 astk[1],即替换的次数不能超过这个值,因此实际上最多能替换 min{astk[1],kcstk[2]cstk[1]} 次。

  记剩余的 k=kmodcstk[2]cstk[1],同理对于 stk[3],我们应该用 stk[2] 来替换 min{astk[2],kcstk[3]cstk[2]} 次。而不用再考虑用 stk[1] 来替换,这是因为将 stk[1] 替换成 stk[3],等价于先将 stk[1] 替换成 stk[2],再将 stk[2] 替换成 stk[3],两种替换方式的花费是一样的。而上一步已经通过最大次数来将 stk[1] 替换成 stk[2],以使得 a 的字典序尽可能大,因此 k 是无法再将 stk[1] 替换成 stk[2],因此不足以将 stk[1] 替换成 stk[3]

  以此类推,可以发现每一次只需将前一个元素替换即可。

  AC 代码如下,时间复杂度为 O(n)

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int a[N];
int stk[N], tp;
int ans[N];

void solve() {
    int n, m;
    scanf("%d", &n);
    tp = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        while (tp && a[i] <= a[stk[tp]]) {    // 删掉下标小于i且花费大于等于a[i]的元素
            tp--;
        }
        stk[++tp] = i;    // 栈里存的是下标
    }
    scanf("%d", &m);
    memset(ans, 0, n + 10 << 2);
    ans[0] = 2e9;
    for (int i = 1; i <= tp; i++) {
        int t = min(ans[stk[i - 1]], m / (a[stk[i]] - a[stk[i - 1]]));    // 最多替换的次数
        ans[stk[i]] = t;
        ans[stk[i - 1]] -= t;
        m -= t * (a[stk[i]] - a[stk[i - 1]]);
    }
    for (int i = n; i; i--) {
        ans[i] += ans[i + 1];
    }
    for (int i = 1; i <= n; i++) {
        printf("%d ", ans[i]);
    }
    printf("\n");
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }

    return 0;
}

 

参考资料

  CodeTON Round 6 (Div. 1 + Div. 2, Rated, Prizes!) Editorial:https://codeforces.com/blog/entry/120524

posted @   onlyblues  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示