Codeforces Key Races Problem in Python and C++

Published January 26, 2024

If you are preparing for USACO or any other competitive programming, Codeforces is a good place to practice your programming. This article contains my Python and C++ solutions for the Key Races problem 835A.

Codeforces Key Races problem

Key Races problem

Please read the Key Races problem on Codeforces. Make sure you understand the problem first. Do not code until you have written the algorithm on paper. For your algorithm, you can write either pseudocode or a diagrammatical flowchart.

Logic

You start with 5 variables, s, v1, v2, t1, t2.

The time taken by the first participant would be time taken for all letters s * v1 plus roundtrip ping time, which would be t1 * 2. The total would be s * v1 + t1 * 2.

Similarly, for the second participant, the total time would be s * v2 + t2 * 2.

Now, using these values, do an if-else condition to return First, Second or Friendship.

Test cases

You can download test cases:

Install Python or C++

If you do not have Python or C++, you should first install it. Codeforces uses Python 3.8 for evaluating your code. If you are using C++, they have versions 17 and 20.

My Python solution

This is my Python solution:

s, v1, v2, t1, t2 = map(int, input().split(' '))

first = s * v1 + t1 * 2
second = s * v2 + t2 * 2
if  first < second:
    print('First')
elif second < first:
    print('Second')
else:
    print('Friendship')

Running the Python code

Run the Python code from your Terminal. I am using macOS, but you can use any Linux-based OS as well.

$ cat key_races_1.in | python key_races.py 
First

$ cat key_races_2.in | python key_races.py 
Second

$ cat key_races_3.in | python key_races.py 
Friendship

My C++ solution

This is my C++ solution:

#include <iostream>

using namespace std;

int main() {
    int s, v1, v2, t1, t2;
    cin >> s >> v1 >> v2 >> t1 >> t2;

    // Compare and find the lowest value guy
    int first, second;
    first = s * v1 + t1 * 2;
    second = s * v2 + t2 * 2;
    if (first < second) {
        cout << "First";
    } else if (second < first) {
        cout << "Second";
    } else {
        cout << "Friendship";
    }
}

Running the C++ code

Compile the C++ code from your Terminal. This command creates a.out.

$ g++ key_races.cpp 

Now, run all the three test cases against a.out.

$ cat key_races_1.in | ./a.out 
First

$ cat key_races_2.in | ./a.out 
Second

$ cat key_races_3.in | ./a.out 
Friendship

Submit your code

Submit your Python code and C++ code to Codeforces. When your code successfully passes all test cases, you will get a green "Accepted" message with the time taken.

Which is faster?

In the Codeforces submission page, these were the results:

Python 3        -- Time taken: 46 ms
GNU C++20 (64)  -- Time taken: 15 ms

What about software design, conventions and standards?

This is competitive programming. The goal is to complete the program with minimum effort and maximum efficiency. Using single character variables and ugly formatted code are okay, as long as the program runs and runs well within the scope. Later in life, when you start working in a professional environment, please beautify your ugly code, follow conventions, add comments and make your code readable.

Conclusion

If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: January 26, 2024.     This post was originally written on January 26, 2024.