The Best Player

It's Lolympics 2016 right now, and we all know who's the best player there right now: Kalyani! Obviously, he has a huge female fan following and he has to make sure they are all happy and rooting for him to win the gold medals.

But with fan following comes arrogance and lack of time. Thus, he has sufficient time to interact with atmost T of his fans. Each fan is defined by two parameters: Name and Fan Quotient. The name defines the name of the fan, while the fan quotient is a measure of the fan's devotion towards Kalyani. The higher the fan quotient, the greater is the devotion. Kalyani now wants to meet T of his fans. While selecting the fans he wants to meet, he wants to make sure that a fan with a higher fan quotient should be given a chance in favor of those with a lesser fan quotient. In the case of ties, he sorts their name lexicographically and chooses the lexicographically lesser named fan.

Given details of N fans, can you help out Kalyani by giving him a list of fans he would be interacting with?

Example:

Input: n = 3, k = 2, v = {{3, "surbhi"}, {3, "surpanakha"}, {5, "shreya"}}

Output:

shreya surbhi

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

bool cmp(pair<long longstringa,
         pair<long longstringb)
{
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
}

void theBestPlayer(long long nlong long k,
                   vector<pair<long longstring>> &v)
{
    sort(v.begin(), v.end(), cmp);
    int cnt = 0;
    for (int i = n - 1i >= 0i--)
    {
        cout << v[i].second << "\n";
        cnt++;
        if (cnt == k)
            break;
    }
}
int main()
{
    long long n = 3k = 2;

    vector<pair<long longstring>> v = {{3"surbhi"},
                                         {3"surpanakha"},
                                         {5"shreya"}};

    theBestPlayer(nkv);

    return 0;
}


No comments:

Post a Comment