WA in MOVIEWKN

https://www.codechef.com/viewsolution/28571980

A bit rushed, but hopefully consider the testcase:

1
6
13 32 26 30 4 26
48 23 37 9 4 37
1 Like

Thanks for helping. I don’t know how you found the error, you are so awesome ! :grinning:

1 Like

Simple solution + random testcase generator :slight_smile:

// Simon St James (ssjgz) - 2019-12-30
// 
// Solution to: https://www.codechef.com/problems/MOVIEWKN
//
#include <iostream>
#include <vector>
#include <algorithm>

#include <cassert>

#include <sys/time.h> // TODO - this is only for random testcase generation.  Remove it when you don't need new random testcases!

using namespace std;

template <typename T>
T read()
{
    T toRead;
    cin >> toRead;
    assert(cin);
    return toRead;
}

int main(int argc, char* argv[])
{
    ios::sync_with_stdio(false);
    if (argc == 2 && string(argv[1]) == "--test")
    {
        struct timeval time;
        gettimeofday(&time,NULL);
        srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
        
        const int T = 1;
        cout << T << endl;

        for (int t = 0; t < T; t++)
        {
            const int N = 1 + rand() % 10;
            const int maxLength = 1 + rand() % 100;
            const int maxRating = 1 + rand() % 100;

            cout << N << endl;

            for (int i = 0; i < N; i++)
            {
                cout << (1 + rand() % maxLength);
                if (i != N - 1)
                    cout << " ";
            }
            cout << endl;
 
            for (int i = 0; i < N; i++)
            {
                cout << (1 + rand() % maxRating);
                if (i != N - 1)
                    cout << " ";
            }       
            cout << endl;
        }

        return 0;
    }
    
    const auto T = read<int>();

    for (int t = 0; t < T; t++)
    {
        struct Movie
        {
            int length = -1;
            int rating = -1;
            int index = -1;
        };

        const int numMovies = read<int>();

        vector<Movie> movies(numMovies);

        int index = 1;
        for (auto& movie : movies)
        {
            movie.length = read<int>();
            movie.index = index;
            index++;
        }

        for (auto& movie : movies)
        {
            movie.rating = read<int>();
        }

        const auto& bestMovie = *std::max_element(movies.begin(),
                                                    movies.end(),
            [](const auto& lhsMovie, const auto& rhsMovie)
            {
                if (lhsMovie.rating * lhsMovie.length != rhsMovie.rating * rhsMovie.length)
                {
                    return lhsMovie.rating * lhsMovie.length < rhsMovie.rating * rhsMovie.length;
                }
                if (lhsMovie.rating != rhsMovie.rating)
                {
                    return lhsMovie.rating < rhsMovie.rating;
                }
                    return lhsMovie.index > rhsMovie.index;
            });

        cout << bestMovie.index << endl;

    }

    assert(cin);
}
2 Likes

thanks:blush:

1 Like