本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Codeforces Round #717 (Div. 2)-A. Tit for Tat
传送门 Time Limit: 1 second Memory Limit: 256 megabytes
Problem Description
Given an array of length , you can do at most operations of the following type on it:
What is lexicographically the smallest array you can obtain?
An array is lexicographically smaller than an array if there exists an index such that , and for all . Less formally, at the first index in which they differ, .
Input
The first line contains an integer () – the number of test cases you need to solve.
The first line of each test case contains integers and (, ) — the number of elements in the array and the maximum number of operations you can make.
The second line contains space-separated integers , , , () — the elements of the array .
Output
For each test case, print the lexicographically smallest array you can obtain after at most 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 from the first element and adding to the second. Then, we can't get any lexicographically smaller arrays, because we can't make any of the elements negative.
题目大意
给你含有个非负整数的数组,你可以对他进行此操作。 每次操作可以选择两个数,并把其中的一个数减一,另一个数加一。 问你最多次操作后,这些数最小字典序是什么样子。
题目分析
要使字典序最小,就要使前面的数尽量小。 但总和不变,因此就要使后面的数尽量大。 所以每次操作,就把尽可能考前的正数减一,最后一个数加一,知道次就行了。
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…