fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5. import java.util.TreeMap;
  6.  
  7. class ARRAYSUB {
  8.  
  9. public static void main(String[] args) throws NumberFormatException, IOException {
  10. // TODO Auto-generated method stub
  11. int n = Integer.parseInt(br.readLine());
  12. // br.readLine();
  13. StringTokenizer st = new StringTokenizer(br.readLine());
  14. long a[] = new long[n];
  15. StringBuilder sb = new StringBuilder();
  16. for (int i = 0; i < n; i++)
  17. a[i] = Long.parseLong(st.nextToken());
  18. TreeMap<Long, Long> tm = new TreeMap<Long, Long>();
  19. int k = Integer.parseInt(br.readLine());
  20. for (int i = 0; i < k; i++) {
  21. Long x = tm.get(a[i]);
  22. tm.put(a[i], x == null ? 1 : x + 1);
  23. }
  24.  
  25. sb.append(tm.lastKey() + " ");
  26. for (int i = k, j = 0; i < n; i++, j++) {
  27. Long x = tm.get(a[j]);
  28. x = x - 1;//note that x will never be null here.
  29. if (x == 0)
  30. tm.remove(a[j]);
  31. else
  32. tm.put(a[j], x);
  33. x = tm.get(a[i]);
  34. tm.put(a[i], x == null ? 1 : x + 1);
  35.  
  36. sb.append(tm.lastKey() + " ");
  37. }
  38. System.out.println(sb);
  39. }
  40.  
  41. }
  42.  
Success #stdin #stdout 0.07s 380224KB
stdin
12
1 2 3 3 1 2 4 5 1 1 2 3
3
stdout
3 3 3 3 4 5 5 5 2 3