fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static class Node
  11. {
  12. char c;int s;
  13. Node(char x,int y)
  14. {
  15. c=x;s=y;
  16. }
  17. }
  18.  
  19. public static void main (String[] args) throws java.lang.Exception
  20. {
  21. TreeSet<Node> ts=new TreeSet<Node>(new Comparator<Node>()
  22. {
  23. @Override
  24. public int compare(Node o1, Node o2)
  25. {
  26. if(o2.s!=o1.s)
  27. {
  28. return o2.s-o1.s;
  29. }
  30. return (o1.c-o2.c);
  31. }
  32. });
  33. ts.add(new Node('a',3));
  34. ts.add(new Node('b',2));
  35. ts.add(new Node('c',1));
  36. while(ts.size()!=0)
  37. {
  38. Node node=ts.pollFirst();
  39. System.out.println(node.c+" "+node.s);//Printing the character and the number associated with ti
  40. if(node.s>1)
  41. {
  42. node.s--;
  43. ts.add(node);
  44. }
  45. }
  46. }
  47. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
a 3
a 2
b 2
a 1
b 1
c 1