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

84 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

@TOC

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

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

Problem Description

Given an array aa of length nn, you can do at most kk operations of the following type on it:

What is lexicographically the smallest array you can obtain?

An array xx is lexicographically smaller than an array yy if there exists an index ii such that xi<yix_i<y_i, and xj=yjx_j=y_j for all 1j<i1 \le j < i. Less formally, at the first index ii in which they differ, xi<yix_i<y_i.

Input

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

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

The second line contains nn space-separated integers a1a_1, a2a_2, \ldots, ana_{n} (0ai1000 \le a_i \le 100) — the elements of the array aa.

Output

For each test case, print the lexicographically smallest array you can obtain after at most kk 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 11 from the first element and adding 11 to the second. Then, we can't get any lexicographically smaller arrays, because we can't make any of the elements negative.


题目大意

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


题目分析

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


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;
}

同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…