Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 1≤j<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 1≤t≤20) – 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 2≤n≤100, 1 ≤ k ≤ 10000 1 \\le k \\le 10000 1≤k≤10000) — 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 0≤ai≤100) — 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
以上是关于Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解
区间倍增dpD. Cut——Codeforces Round #717 (Div. 2)
Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解
Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解
Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解
Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解