Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. package com.muratdozen.playground.spotify;
  2.  
  3. import com.muratdozen.playground.util.io.FastReader;
  4. import com.muratdozen.playground.util.threading.NonThreadSafe;
  5.  
  6. import java.io.PrintWriter;
  7. import java.util.*;
  8.  
  9. /**
  10. * Spotify Challenge, Zipfs Song
  11. *
  12. * @author Murat Derya Ozen
  13. * @since: 9/27/13 9:50 PM
  14. * @see <a href="https://www.spotify.com/us/jobs/tech/zipfsong/">
  15. * https://www.spotify.com/us/jobs/tech/zipfsong/</a>
  16. */
  17. public class ZipfsSong {
  18.  
  19. /**
  20. * FixedSizePriorityQueue is a priority queue implementation with a fixed size based on a {@link PriorityQueue}.
  21. * The number of elements in the queue will be at most {@code maxSize}.
  22. * Once the number of elements in the queue reaches {@code maxSize}, trying to add a new element
  23. * will remove the lowest element in the queue if the new element is greater than or equal to
  24. * the current lowest element. The queue will not be modified otherwise.
  25. */
  26. @NonThreadSafe
  27. public static class FixedSizePriorityQueue<E> {
  28. private final PriorityQueue<E> priorityQueue; /* backing data structure */
  29. private final Comparator<? super E> comparator;
  30. private final int maxSize;
  31.  
  32. /**
  33. * Constructs a {@link FixedSizePriorityQueue} with the specified {@code maxSize}
  34. * and {@code comparator}.
  35. *
  36. * @param maxSize - The maximum size the queue can reach, must be a positive integer.
  37. * @param comparator - The comparator to be used to compare the elements in the queue, must be non-null.
  38. */
  39. public FixedSizePriorityQueue(final int maxSize, final Comparator<? super E> comparator) {
  40. super();
  41. if (maxSize <= 0) {
  42. throw new IllegalArgumentException("maxSize = " + maxSize + "; expected a positive integer.");
  43. }
  44. if (comparator == null) {
  45. throw new NullPointerException("Comparator is null.");
  46. }
  47. this.priorityQueue = new PriorityQueue<E>(maxSize, comparator);
  48. this.comparator = priorityQueue.comparator();
  49. this.maxSize = maxSize;
  50. }
  51.  
  52. /**
  53. * Adds an element to the queue. If the queue contains {@code maxSize} elements, {@code e} will
  54. * be compared to the lowest element in the queue using {@code comparator}.
  55. * If {@code e} is greater than or equal to the lowest element, that element will be removed and
  56. * {@code e} will be added instead. Otherwise, the queue will not be modified
  57. * and {@code e} will not be added.
  58. *
  59. * @param e - Element to be added, must be non-null.
  60. */
  61. public void add(final E e) {
  62. if (e == null) {
  63. throw new NullPointerException("e is null.");
  64. }
  65. if (maxSize <= priorityQueue.size()) {
  66. final E firstElm = priorityQueue.peek();
  67. if (comparator.compare(e, firstElm) < 1) {
  68. return;
  69. } else {
  70. priorityQueue.poll();
  71. }
  72. }
  73. priorityQueue.add(e);
  74. }
  75.  
  76. /**
  77. * To be not used. Not an efficient operation.
  78. * @return Returns a sorted view of the queue as a {@link Collections#unmodifiableList(java.util.List)}
  79. * unmodifiableList.
  80. */
  81. public List<E> asList() {
  82. return Collections.unmodifiableList(new ArrayList<E>(priorityQueue));
  83. }
  84. }
  85.  
  86. /**
  87. * Represents a Song where all fields are public and final.
  88. */
  89. public static class Song {
  90.  
  91. public static final Comparator<? super Song> COMPARATOR = new Comparator<Song>() {
  92. @Override
  93. public int compare(Song song1, Song song2) {
  94. if (song1.quality == song2.quality) {
  95. return -Integer.valueOf(song1.order).compareTo(song2.order);
  96. }
  97. return Long.valueOf(song1.quality).compareTo(song2.quality);
  98. }
  99. };
  100.  
  101. public final long quality;
  102. public final int order;
  103. public final String name;
  104.  
  105. /**
  106. * @param quality - Quality of the song must be a non-negative integer.
  107. * @param order - Order of the song (as it appears in the album) must be a positive integer.
  108. * @param name - Name of the song must be non-null.
  109. */
  110. public Song(final long quality, final int order, final String name) {
  111. super();
  112. if (quality < 0) {
  113. throw new IllegalArgumentException("quality = " + quality + "; expected a non-negative integer.");
  114. }
  115. if (order <= 0) {
  116. throw new IllegalArgumentException("order = " + order + "; expected a positive integer.");
  117. }
  118. if (name == null) {
  119. throw new NullPointerException("Name is null.");
  120. }
  121. this.quality = quality;
  122. this.order = order;
  123. this.name = name;
  124. }
  125. }
  126.  
  127. private static final void solveUsingFixedPriorityQueue() {
  128. final FastReader reader = FastReader.from(System.in);
  129. final int N = reader.nextInt();
  130. final int M = reader.nextInt();
  131.  
  132. final FixedSizePriorityQueue<Song> songsQueue = new FixedSizePriorityQueue<Song>(M,
  133. Song.COMPARATOR);
  134.  
  135. // process each song, add to queue
  136. for (int i = 1; i <= N; ++i) {
  137. final long hits = reader.nextLong();
  138. final long quality = hits * i;
  139. final String name = reader.next();
  140. songsQueue.add(new Song(quality, i, name));
  141. }
  142.  
  143. reader.close();
  144.  
  145. // output the results in descending order
  146. final String[] descendingSongNames = new String[M];
  147. int i = M - 1;
  148. Song song = null;
  149. while ((song = songsQueue.priorityQueue.poll()) != null) {
  150. descendingSongNames[i--] = song.name;
  151. }
  152.  
  153. final PrintWriter writer = new PrintWriter(System.out);
  154. final String LINE_SEPARATOR = System.getProperty("line.separator");
  155. for (String str : descendingSongNames) {
  156. writer.write(str);
  157. writer.write(LINE_SEPARATOR);
  158. }
  159.  
  160. writer.flush();
  161. writer.close();
  162.  
  163. }
  164.  
  165. public static final void main(String[] args) {
  166. solveUsingFixedPriorityQueue();
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement