Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解

Codeforces Round #717 (Div. 2)-A. Tit for Tat

传送门
Time Limit: 1 second
Memory Limit: 256 megabytes

Problem Description

Given an array a a a of length n n n, you can do at most k k k operations of the following type on it:

What is lexicographically the smallest array you can obtain?

An array x x x is lexicographically smaller than an array y y y if there exists an index i i i such that x i < y i x_i<y_i xi<yi, and x j = y j x_j=y_j xj=yj for all 1 ≤ j < i 1 \le j < i 1j<i. Less formally, at the first index i i i in which they differ, x i < y i x_i<y_i xi<yi.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 20 1 \le t \le 20 1t20) – the number of test cases you need to solve.

The first line of each test case contains 2 2 2 integers n n n and k k k ( 2 ≤ n ≤ 100 2 \le n \le 100 2n100, 1 ≤ k ≤ 10000 1 \le k \le 10000 1k10000) — the number of elements in the array and the maximum number of operations you can make.

The second line contains n n n space-separated integers a 1 a_1 a1, a 2 a_2 a2, … \ldots , a n a_{n} an ( 0 ≤ a i ≤ 100 0 \le a_i \le 100 0ai100) — the elements of the array a a a.

Output

For each test case, print the lexicographically smallest array you can obtain after at most k k k operations.

Sample Input

2
3 1
3 1 4
2 10
1 0

Sample Onput

2 1 5 
0 1 

Note

In the second test case, we start by subtracting 1 1 1 from the first element and adding 1 1 1 to the second. Then, we can’t get any lexicographically smaller arrays, because we can’t make any of the elements negative.


题目大意

给你含有 n n n个非负整数的数组,你可以对他进行 k k k此操作。
每次操作可以选择两个数,并把其中的一个数减一,另一个数加一。
问你最多 k k k次操作后,这些数最小字典序是什么样子。


题目分析

要使字典序最小,就要使前面的数尽量小。
但总和不变,因此就要使后面的数尽量大。
所以每次操作,就把尽可能考前的正数减一,最后一个数加一,知道 k k k次就行了。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
int a[1010];
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n,k;
        cin>>n>>k;
        fi(i,0,n)//for(int i=0;i<n;i++)
            cd(a[i]);//scanf("%d", &a[i]);
        int loc=0;//下标从第一个元素开始
        while(loc<n-1&&k>0)//第一个大于0的数不是最后一个 且 还有剩余的操作次数
            if(a[loc])//如果这个数不是0
                a[loc]--,a[n-1]++,k--;//这个数-1,最后一个数+1,剩余操作次数-1
            else//否则这个数是0
                loc++;//下标加一,下次处理下一个数
        fi(i,0,n)
            printf("%d ",a[i]);
        puts("");//换行
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/116354382

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tisfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值