-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzipfsong.cpp
More file actions
70 lines (61 loc) · 2.11 KB
/
zipfsong.cpp
File metadata and controls
70 lines (61 loc) · 2.11 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//*******************************************
// Spotify puzzle #2 - Zipf's song
// Gustav Andersson - 161015
// gustav.k.andersson@gmail.com
//*******************************************
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <chrono>
class Song
{
public:
Song(const std::string_view& name, int64_t rating) : _name(name), _zipf_rating(rating) {} ;
~Song() = default;
const std::string& name() const {return _name;} ;
int64_t rating() const {return _zipf_rating;} ;
friend bool operator< (const Song& lhs, const Song& rhs){ return lhs._zipf_rating < rhs._zipf_rating; };
friend bool operator> (const Song& lhs, const Song& rhs){ return rhs < lhs; };
private:
std::string _name;
int64_t _zipf_rating;
};
void parse_songs(int songs, std::vector<Song>& song_list)
{
song_list.reserve(songs);
std::string input;
for (int i = 1 ; i <= songs ; i++)
{
std::getline(std::cin, input);
int pos = input.find(" ");
int64_t listens = atol(input.substr(0, pos).c_str());
song_list.emplace_back(std::string_view(input).substr(pos + 1), listens * i);
}
return;
}
void print_songs(const std::vector<Song>& song_list, int no_songs)
{
for (int i = 0; i < no_songs ; ++i)
{
std::cout << song_list[i].name() << '\n';
}
return;
}
int main()
{
std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();
std::ios::sync_with_stdio (false);
std::string input;
std::getline(std::cin, input);
int pos = input.find(" ");
int no_songs = atoi(input.substr(0, pos).c_str());
int no_outputs = atoi(input.substr(pos + 1).c_str());
std::vector<Song> song_list;
parse_songs(no_songs, song_list);
stable_sort(song_list.begin(), song_list.end(), std::greater<Song>());
print_songs(song_list, no_outputs);
std::chrono::steady_clock::time_point endTime = std::chrono::steady_clock::now();
std::cout << "Ex. time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << " ms\n";
return 0;
}