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 long, string> a,pair<long long, string> b){if (a.first == b.first)return a.second > b.second;return a.first < b.first;}void theBestPlayer(long long n, long long k,vector<pair<long long, string>> &v){sort(v.begin(), v.end(), cmp);int cnt = 0;for (int i = n - 1; i >= 0; i--){cout << v[i].second << "\n";cnt++;if (cnt == k)break;}}int main(){long long n = 3, k = 2;vector<pair<long long, string>> v = {{3, "surbhi"},{3, "surpanakha"},{5, "shreya"}};theBestPlayer(n, k, v);return 0;}
No comments:
Post a Comment