-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathZipfsSong.java
96 lines (79 loc) · 2.66 KB
/
ZipfsSong.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// link: https://labs.spotify.com/puzzles/
// name: Zipfs Song
package kattis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeSet;
/**
* Created by mislav on 11/18/14.
*/
public class ZipfsSong {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
List<Song> songs = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
long listenedToCount = Long.parseLong(st.nextToken());
String name = st.nextToken();
songs.add(new Song(name, listenedToCount, i + 1));
}
TreeSet<Song> result = new TreeSet<kattis.ZipfsSong.Song>(new Comparator<Song>() {
@Override
public int compare(Song song, Song song2) {
long listenedExpectedCompare = song2.getListenedToCount() * song2.getOrder() - song.getListenedToCount() * song.getOrder();
if (listenedExpectedCompare == 0) {
return (int) (song.getOrder() - song2.getOrder());
}
if (listenedExpectedCompare < 0)
return -1;
return 1;
}
});
for (Song song : songs){
result.add(song);
if(result.size() > m){
result.pollLast();
}
}
for (Song current : result) {
System.out.println(current.getName());
}
br.close();
}
public static class Song {
private String name;
private long listenedToCount;
private long order;
public Song(String name, long listenedToCount, long order) {
this.name = name;
this.listenedToCount = listenedToCount;
this.order = order;
}
public String getName() {
return name;
}
public long getListenedToCount() {
return listenedToCount;
}
public long getOrder() {
return order;
}
@Override
public String toString() {
return "Song{" +
"name='" + name + '\'' +
", listenedToCount=" + listenedToCount +
", order=" + order +
'}';
}
}
}