HackerEarth Roy and Trending Topics problem solution

In this HackerEarth Roy and Trending Topics problem solution, Roy is trying to develop a widget that shows Trending Topics (similar to Facebook) on the home page of HackerEarth Academy.
He has gathered a list of N Topics (their IDs) and their popularity score (say z-score) from the database. Now z-score change every day according to the following rules:
  1. When a topic is mentioned in a 'Post', its z-score is increased by 50.
  2. A 'Like' on such a Post, increases the z-score by 5.
  3. A 'Comment' increases the z-score by 10.
  4. A 'Share' causes an increment of 20.
Now the Trending Topics are decided according to the change in z-score. One with the highest increment comes on top and the list follows.
Roy seeks your help to write an algorithm to find the top 5 Trending Topics.
If a change in z-score for any two topics is the same, then rank them according to their ID (one with higher ID gets priority). It is guaranteed that IDs will be unique.

HackerEarth Roy and Trending Topics problem solution

HackerEarth Roy and Trending Topics problem solution.

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
using namespace std;
vector < pair<long long, long long> > topics;
map<long long,long long> id_zscore_map;
int main()
int n;
long long id, z, p, l, c, s;
scanf(" %d",&n);
for(int i=0;i<n;i++)
scanf(" %lld %lld %lld %lld %lld %lld",&id,&z,&p,&l,&c,&s);
topics.push_back(make_pair( (p*50 + l*5 + c*10 + s*20 - z) , id));
id_zscore_map[id] = z;
make_heap(topics.begin(), topics.end());
for(int i=0;i<5;i++)
printf("%lld %lld\n",topics.front().second, id_zscore_map[topics.front().second] + topics.front().first);
pop_heap (topics.begin(),topics.end()); topics.pop_back();
return 0;

Post a Comment