← 返回 bytedance 的题目列表Top-K KOL by Like Count (Heap Optimization)
类型:online_judge
Problem: Top-K KOLs by Total Likes (Heap Optimization)
You are given a list of like events. Each event contributes some number of likes to a KOL (creator). Compute total likes per KOL and output the top K KOLs with the highest total likes.
Input
First line: two integers N and K
N = number of events
K = number of KOLs to output
Next N lines: kol_id like_count
kol_id is a string (or integer)
like_count is a non-negative integer
Output
Print the top K KOLs sorted by total_likes descending; if tied, sort by kol_id ascending. Each line: kol_id total_likes
Constraints
1 <= N <= 2e5
1 <= K <= min(1e5, number of distinct kol_id)
0 <= like_count <= 1e9
Aim for O(N log K) using a size-K min-heap (after aggregation).
Example
Input:
6 2
A 1
B 5
A 3
C 2
B 1
D 10
Output:
D 10
B 6
Example
Input
6 2
A 1
B 5
A 3
C 2
B 1
D 10
Output
D 10
B 6