Google Kickstart Round A 2019 - Training

What's the purpose of "true" in bash "if sudo true; then"

Is there a good way to store credentials outside of a password manager?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Is there a problem with hiding "forgot password" until it's needed?

Implement the Thanos sorting algorithm

Is a roofing delivery truck likely to crack my driveway slab?

What is the opposite of 'gravitas'?

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

What's a natural way to say that someone works somewhere (for a job)?

There is only s̶i̶x̶t̶y one place he can be

Is there an Impartial Brexit Deal comparison site?

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

Minimal reference content

Select empty space and change color in vector

Personal Teleportation as a Weapon

Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past

What will be the benefits of Brexit?

Have I saved too much for retirement so far?

The Riley Riddle Mine

Why are on-board computers allowed to change controls without notifying the pilots?

Opposite of a diet

What is the term when two people sing in harmony, but they aren't singing the same notes?

Go Pregnant or Go Home

Failed to fetch jessie backports repository



Google Kickstart Round A 2019 - Training














0












$begingroup$


Problem: Training

As the football coach at your local school, you have been tasked with picking a team of exactly P students to represent your school. There are N students for you to pick from. The i-th student has a skill rating Si, which is a positive integer indicating how skilled they are.



You have decided that a team is fair if it has exactly P students on it and they all have the same skill rating. That way, everyone plays as a team. Initially, it might not be possible to pick a fair team, so you will give some of the students one-on-one coaching. It takes one hour of coaching to increase the skill rating of any student by 1.



The competition season is starting very soon (in fact, the first match has already started!), so you'd like to find the minimum number of hours of coaching you need to give before you are able to pick a fair team.



Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing the two integers N and P, the number of students and the number of students you need to pick, respectively. Then, another line follows containing N integers Si; the i-th of these is the skill of the i-th student.



Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of hours of coaching needed, before you can pick a fair team of P students.



Limits

Time limit: 15 seconds per test set.

Memory limit: 1 GB.

1 ≤ T ≤ 100.

1 ≤ Si ≤ 10000, for all i.

2 ≤ P ≤ N.

Test set 1 (Visible)

2 ≤ N ≤ 1000.

Test set 2 (Hidden)

2 ≤ N ≤ 105.



Sample



Input



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


Output



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


In Sample Case #1, you can spend a total of 6 hours training the first student and 8 hours training the second one. This gives the first, second and third students a skill level of 9. This is the minimum time you can spend, so the answer is 14.



In Sample Case #2, you can already pick a fair team (the first and second student) without having to do any coaching, so the answer is 0.



In Sample Case #3, P = N, so every student will be on your team. You have to spend 6 hours training the third student, so that they have a skill of 7, like everyone else. This is the minimum time you can spend, so the answer is 6.



My solution



#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

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



int minTrainingTime(std::vector<int> &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 remainingPossibilities = 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<int> skills;
getData(p, skills);

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




Analysis



Time complexity: O(n*log(n)), dominated by sorting

Space complexity: O(1), not counting the original data given by the problem









share







New contributor




Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    0












    $begingroup$


    Problem: Training

    As the football coach at your local school, you have been tasked with picking a team of exactly P students to represent your school. There are N students for you to pick from. The i-th student has a skill rating Si, which is a positive integer indicating how skilled they are.



    You have decided that a team is fair if it has exactly P students on it and they all have the same skill rating. That way, everyone plays as a team. Initially, it might not be possible to pick a fair team, so you will give some of the students one-on-one coaching. It takes one hour of coaching to increase the skill rating of any student by 1.



    The competition season is starting very soon (in fact, the first match has already started!), so you'd like to find the minimum number of hours of coaching you need to give before you are able to pick a fair team.



    Input

    The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing the two integers N and P, the number of students and the number of students you need to pick, respectively. Then, another line follows containing N integers Si; the i-th of these is the skill of the i-th student.



    Output

    For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of hours of coaching needed, before you can pick a fair team of P students.



    Limits

    Time limit: 15 seconds per test set.

    Memory limit: 1 GB.

    1 ≤ T ≤ 100.

    1 ≤ Si ≤ 10000, for all i.

    2 ≤ P ≤ N.

    Test set 1 (Visible)

    2 ≤ N ≤ 1000.

    Test set 2 (Hidden)

    2 ≤ N ≤ 105.



    Sample



    Input



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


    Output



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


    In Sample Case #1, you can spend a total of 6 hours training the first student and 8 hours training the second one. This gives the first, second and third students a skill level of 9. This is the minimum time you can spend, so the answer is 14.



    In Sample Case #2, you can already pick a fair team (the first and second student) without having to do any coaching, so the answer is 0.



    In Sample Case #3, P = N, so every student will be on your team. You have to spend 6 hours training the third student, so that they have a skill of 7, like everyone else. This is the minimum time you can spend, so the answer is 6.



    My solution



    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <limits>

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



    int minTrainingTime(std::vector<int> &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 remainingPossibilities = 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<int> skills;
    getData(p, skills);

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




    Analysis



    Time complexity: O(n*log(n)), dominated by sorting

    Space complexity: O(1), not counting the original data given by the problem









    share







    New contributor




    Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $begingroup$


      Problem: Training

      As the football coach at your local school, you have been tasked with picking a team of exactly P students to represent your school. There are N students for you to pick from. The i-th student has a skill rating Si, which is a positive integer indicating how skilled they are.



      You have decided that a team is fair if it has exactly P students on it and they all have the same skill rating. That way, everyone plays as a team. Initially, it might not be possible to pick a fair team, so you will give some of the students one-on-one coaching. It takes one hour of coaching to increase the skill rating of any student by 1.



      The competition season is starting very soon (in fact, the first match has already started!), so you'd like to find the minimum number of hours of coaching you need to give before you are able to pick a fair team.



      Input

      The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing the two integers N and P, the number of students and the number of students you need to pick, respectively. Then, another line follows containing N integers Si; the i-th of these is the skill of the i-th student.



      Output

      For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of hours of coaching needed, before you can pick a fair team of P students.



      Limits

      Time limit: 15 seconds per test set.

      Memory limit: 1 GB.

      1 ≤ T ≤ 100.

      1 ≤ Si ≤ 10000, for all i.

      2 ≤ P ≤ N.

      Test set 1 (Visible)

      2 ≤ N ≤ 1000.

      Test set 2 (Hidden)

      2 ≤ N ≤ 105.



      Sample



      Input



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


      Output



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


      In Sample Case #1, you can spend a total of 6 hours training the first student and 8 hours training the second one. This gives the first, second and third students a skill level of 9. This is the minimum time you can spend, so the answer is 14.



      In Sample Case #2, you can already pick a fair team (the first and second student) without having to do any coaching, so the answer is 0.



      In Sample Case #3, P = N, so every student will be on your team. You have to spend 6 hours training the third student, so that they have a skill of 7, like everyone else. This is the minimum time you can spend, so the answer is 6.



      My solution



      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <limits>

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



      int minTrainingTime(std::vector<int> &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 remainingPossibilities = 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<int> skills;
      getData(p, skills);

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




      Analysis



      Time complexity: O(n*log(n)), dominated by sorting

      Space complexity: O(1), not counting the original data given by the problem









      share







      New contributor




      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      Problem: Training

      As the football coach at your local school, you have been tasked with picking a team of exactly P students to represent your school. There are N students for you to pick from. The i-th student has a skill rating Si, which is a positive integer indicating how skilled they are.



      You have decided that a team is fair if it has exactly P students on it and they all have the same skill rating. That way, everyone plays as a team. Initially, it might not be possible to pick a fair team, so you will give some of the students one-on-one coaching. It takes one hour of coaching to increase the skill rating of any student by 1.



      The competition season is starting very soon (in fact, the first match has already started!), so you'd like to find the minimum number of hours of coaching you need to give before you are able to pick a fair team.



      Input

      The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing the two integers N and P, the number of students and the number of students you need to pick, respectively. Then, another line follows containing N integers Si; the i-th of these is the skill of the i-th student.



      Output

      For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of hours of coaching needed, before you can pick a fair team of P students.



      Limits

      Time limit: 15 seconds per test set.

      Memory limit: 1 GB.

      1 ≤ T ≤ 100.

      1 ≤ Si ≤ 10000, for all i.

      2 ≤ P ≤ N.

      Test set 1 (Visible)

      2 ≤ N ≤ 1000.

      Test set 2 (Hidden)

      2 ≤ N ≤ 105.



      Sample



      Input



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


      Output



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


      In Sample Case #1, you can spend a total of 6 hours training the first student and 8 hours training the second one. This gives the first, second and third students a skill level of 9. This is the minimum time you can spend, so the answer is 14.



      In Sample Case #2, you can already pick a fair team (the first and second student) without having to do any coaching, so the answer is 0.



      In Sample Case #3, P = N, so every student will be on your team. You have to spend 6 hours training the third student, so that they have a skill of 7, like everyone else. This is the minimum time you can spend, so the answer is 6.



      My solution



      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <limits>

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



      int minTrainingTime(std::vector<int> &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 remainingPossibilities = 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<int> skills;
      getData(p, skills);

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




      Analysis



      Time complexity: O(n*log(n)), dominated by sorting

      Space complexity: O(1), not counting the original data given by the problem







      c++ performance c++11





      share







      New contributor




      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 mins ago









      EricEric

      101




      101




      New contributor




      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Eric is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216292%2fgoogle-kickstart-round-a-2019-training%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Eric is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Eric is a new contributor. Be nice, and check out our Code of Conduct.












          Eric is a new contributor. Be nice, and check out our Code of Conduct.











          Eric is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216292%2fgoogle-kickstart-round-a-2019-training%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订