/
ZipfSong.cpp
51 lines (42 loc) · 932 Bytes
/
ZipfSong.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Sporify Puzzles, Zipf's Song
* https://gist.github.com/morazow/4966950
*
* Solution
* - stable sorting
*
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N, M;
struct song {
unsigned long long value;
int order;
string name;
song() {}
song(unsigned long long v, int idx, string _name) :
value(v), order(idx), name(_name) { }
};
vector<song> songs;
bool compare(const song a, const song b) {
if (a.value == b.value)
return a.order < b.order;
return a.value > b.value;
}
int main() {
unsigned long long v;
char name[50];
scanf("%d%d\n", &N, &M);
songs.reserve(N);
for (int i = 0; i < N; i++) {
scanf("%llu %s\n", &v, name);
songs.push_back(song(v * (i+1), i, string(name)));
}
sort(songs.begin(), songs.end(), compare);
for (int i = 0; i < M; i++)
printf("%s\n", songs[i].name.c_str());
return 0;
}