Make k Equal from Codeforces Round #629 (Div. 3)

Posted reshuffle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Make k Equal from Codeforces Round #629 (Div. 3)相关的知识,希望对你有一定的参考价值。

description

you are given an array and you are asked to make (k) elements of it equal after some operations.

you can make one of the following operations:

  • take one of the minimum and increase its value by (1)
  • take one of the maximum and subtract it from (1)

find the minimum number of operations to make it possible.

breakthough

this problem involves nothing but implimention. but we can make it elegantly.

let conclusion comes first, our required answer is
(ans=min((ansl,ansr),ansmid)) ,where:

  • (ansl) is the needed operations if we choose to share value (a[k]) with no less than (k) elements.
  • (ansr) is the needed operations if we choose to share value (a[n-k+1]) with no less than (k) elements.
  • (ansmid) is the needed operations if we choose to let (k) elements staying right in the middle be the chosen ones(who is exactly among the k numbers)

most of the times our answer is contributed by (ansl) or (ansr) except some cases like test case number two given by the host, in which both the minimum and maximum are extremely close as well as the chosen one is in between.

apart from these there are also some tricks here related to the constraints of the applications of each answer, which is amazing to generate or come out.

code designed by BIG BROTHER COW to take a reference:https://codeforces.com/contest/1328/submission/74425585

以上是关于Make k Equal from Codeforces Round #629 (Div. 3)的主要内容,如果未能解决你的问题,请参考以下文章

CF-1328 F. Make k Equal

Codeforces 1328F Make k Equal

C. Make It Equal

Codeforces Round #629 (Div. 3) F - Make k Equal (离散化 树状数组维护前缀和)

Codeforces 1065C Make It Equal(差分+贪心)

CF1065C Make It Equal