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

原文地址:https://www.cnblogs.com/reshuffle/p/12588688.html

时间: 2024-10-09 16:05:57

Make k Equal from Codeforces Round #629 (Div. 3)的相关文章

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

https://codeforces.com/contest/1328/problem/F 首先把a数组处理成pair对(num,cnt),表示数字num有cnt个,然后按num升序排序离散化一下. 对于一个数x,若想使得小于x的数字都变成x,必须先把所有小于x的数变成x-1,然后再+1变成x. 同理,要使得大于x的数变成x,必须把所有大于x的数字变成x+1,然后再-1变成x. 以上是题意所要求的必须操作. 思路: 1. 用f[i]数组记录离散化后前i大的数字的总数,那么对于任意第i大数字,可以

Codeforces Round #629 (Div. 3) D. Carousel(思维/贪心?)

The round carousel consists of nn figures of animals. Figures are numbered from 11 to nn in order of the carousel moving. Thus, after the nn -th figure the figure with the number 11 follows. Each figure has its own type — the type of the animal corre

Codeforces Round #629 (Div. 3) E. Tree Queries(LCA)

https://codeforces.com/contest/1328/problem/E 题目所描述的是一棵树,题中已明示1为root结点. 题目可以转化为,是否存在一条路径,满足集合中的k个点到路径的距离小于等于1? 思路: 1.首先倍增离线预处理出结点深度,便于后续在线询问LCA 2.对于每次的询问,依次扫描k个点.对于集合中的u和v两点,每次我们求出u和v的LCA,计算u和v到LCA的距离,如果u和v到LCA的距离同时大于1,那么说明无法找到一条路径,使得u和v到该路径链的距离小于等于1

C. Almost Equal ( Codeforces Round #580 (Div. 2) )

output standard output You are given integer nn. You have to arrange numbers from 11 to 2n2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every nn consecutive numbers on the circle write the

Codeforces Round #629 (Div. 3)

A Divisibility Problem 题意 给你两个正整数\(a,b\) 你每次可以执行一次\(a++\)操作,问你最小的操作步数,使得\(a\)能被\(b\)整除 思路 数学 分情况讨论 当\(a<b\) 时,显然只有\(a == b\)时才能保证\(a\)被\(b\)整除 当\(a>b\) 时,只要把\(a\)调整到离\(kb\)最近的一个\(b\)的倍数,即\(\lceil {a / b}\rceil * b - a\) #include <bits/stdc++.h>

Codeforces Round #629 (Div. 3) E. Tree Queries(lca题)

https://codeforces.com/contest/1328/problem/E E. Tree Queries You are given a rooted tree consisting of nn vertices numbered from 11 to nn. The root of the tree is a vertex number 11. A tree is a connected undirected graph with n−1n−1 edges. You are

Codeforces Round #629 (Div. 3) 解题报告

A [题意]签到题 B [题意]长度为n的字符串,由n-2个a和2个b组成,按照字典序排序,问第k个 [题解]读题细心,最开始以为是ab代表01,搞二进制转化,实际上上面是正确题意,直接算b的位置即可 C [题意]长度为n的由0,1,2组成的序列,分成两个数字,让他们的最小值最大 [题解]所以我们只需要让他尽量平均分即可,0可以分成0和0,2可以分成1和1,所以我们只有当碰到1的时候开始特殊处理,所以我们把1给一个数,然后下来所有的1和2都分配给另外一个数,以此能够保证分配的平均 D [题意]给

D. Carousel.(简单做法)Codeforces.Round #629 (Div. 3)

题目大意 给一个数组,入伙前后两个元素值不同则必须让他们染不同的颜色,问你如何染色能使得使用的颜色种类最少 做法 用贪心的思路,从前往后扫一遍数组按照题目中的规则,如果与前一个元素相同就染同样的颜色,不同就染不一样的,但是只染1,2两种颜色. 由于是个环,最后只需要判断一下最后一个元素是不是和第一个元素不同的情况下染了相同的颜色,如果是,就看数组中有没有染了相同的颜色还挨在一起,如果有,把后面一个元素的颜色换成与前一个元素不同的,并且后面所有的元素都换成与原来相反的 x%2+1 :正常输出即可.

Codeforces Round #486 (Div. 3) C. Equal Sums

Codeforces Round #486 (Div. 3) C. Equal Sums 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/C Description You are given k sequences of integers. The length of the i-th sequence equals to ni. You have to choose exactly two sequences