# c ++ – Google Kickstart Round A 2019 – Training

Problem: training
As a football coach at your local school, you have been instructed to choose a team of P students to represent your school. There are N students to choose from. The i-th student has a skill rating of Si, which is a positive integer indicating his skill level.

You have decided that a team is right if it counts exactly P students and that it has the same rankings. In this way, everyone plays as a team. Initially, it may not be possible to choose a fair team, so you will need to give some students individual coaching. It takes one hour of coaching to increase the skills assessment of every student by one.

The competition season starts very soon (in fact, the first match has already begun!), So you want to find the minimum number of hours of coaching you need to give before you can choose a fair team.

Contribution
The first line of the entry indicates the number of tests, followed by the TT tests. Each test case starts with a line containing the two integers N and P, the number of students and the number of students to be selected. , respectively. Then another line follows containing N integers Si; the i-th of them is the skill of the i-th student.

Exit
For each test case, specify a line containing case # x: y, where x is the test case number (from 1) and y is the minimum number of hours of coaching required before you can choose a case. good team of P students.

limitations
Time limit: 15 seconds per series of tests.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
1 ≤ If ≤ 10000, for all i.
2 ≤ P ≤ N.
Test set 1 (visible)
2 ≤ N ≤ 1000.
Test set 2 (Hidden)
2 ≤ N ≤ 105.

Sample

Contribution

``````3
4 3
3 1 9 100
6 2
5 5 1 2 3 4
5 5
7 7 1 7 7
``````

Exit

``````Case 1: 14
Case 2: 0
Case 3: 6
``````

In case example 1, you can spend a total of 6 hours training the first student and 8 hours training the second. This gives the first, second and third students a Skill Level of 9. This is the minimum time you can spend. The answer is 14.

In case example 2, you can already choose a right team (the first and the second student) without having to coach. The answer is 0.

In the typical case # 3, P = N, so that each student is part of your team. You must spend 6 hours training the third student so that he has a 7 hour skill like everyone else. This is the minimum time you can spend, so the answer is 6.

My solution

``````#understand
#understand
#understand
#understand

void getData (int & p, std :: vector & skills) {
int n;
std :: cin >> n >> p;
for (int i = 0; i < n; i++) {
int skill = 0;
std::cin >> competence;
skills.emplace_back (skill);
}
}

int minTrainingTime (std :: vector and skills, int p) {
std :: sort (skills.begin (), skills.end ());
int low = 0, high = p - 1;

int currentResult = 0;
for (int i = 0; i <high; i ++) {
currentResult + = skills[high] - skills[i];
}

int minResult = currentResult;
int remainsPossibilities = skills.size () - p;
for (int i = 0; i < remainingPossibilities; i++) {
currentResult -= skills[++high] - skills[low++];
currentResult += p * (skills[high] - skills[high - 1]);
minResult = std::min(minResult, currentResult);
}

return minResult;
}

int main() {
int t = 0;
std::cin >> t;
for (int testCase = 0; testCase <t; testCase ++) {
int p;
std :: vector skills;
getData (p, skills);

std :: cout << "Case #" << testCase + 1 << ":" << minTrainingTime (skills, p) << std :: endl;
}
}
``````

Analysis

Temporal complexity: $$O (n log n)$$, dominated by sorting

Complexity of space: $$O (1)$$, not counting the original data given by the problem