Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Created by krngrvr09 on 30/1/15.
  8. */
  9. public class OptimumDP {
  10. public static void main(String[] args){
  11. /*
  12. Inputs are of the format:
  13. 3(number of inputs)
  14. (start_time, end_time, weight)
  15. 7 8 9
  16. 4 5 6
  17. 1 2 3
  18. */
  19. Scanner scanner = new Scanner(System.in);
  20. ArrayList<SingleInputDP> inputs = new ArrayList<SingleInputDP>();
  21. int number_of_inputs = scanner.nextInt();
  22. int[] ps = new int[number_of_inputs];
  23. int[] opt = new int[number_of_inputs];
  24.  
  25.  
  26. //ps[j] is the largest index i(which is smaller than j), such that job i is compatible with j.
  27. //Being compatible means, the 2 jobs do not overlap.
  28.  
  29. Arrays.fill(ps, -1);//initializing ps with -1
  30. Arrays.fill(opt, -1);//initializing ps with -1
  31. System.out.println(number_of_inputs);
  32. for(int i=0;i<number_of_inputs;i++){
  33. int start = scanner.nextInt();
  34. int end = scanner.nextInt();
  35. int weight = scanner.nextInt();
  36. inputs.add(new SingleInputDP(start,end,weight));
  37. }
  38. //Sorting in ascending order with respect to the ending times
  39. inputs.sort(new Comparator<SingleInputDP>() {
  40. @Override
  41. public int compare(SingleInputDP o1, SingleInputDP o2) {
  42. if(o1.end>=o2.end){
  43. return 1;
  44. }
  45. else{
  46. return -1;
  47. }
  48. }
  49. });
  50.  
  51. print_inputs(inputs);
  52. // Populating the ps array. Definition provided above.
  53. for(int i=0;i<inputs.size();i++){
  54. for(int j=0;j<i;j++){
  55. System.out.println(inputs.get(i).start+"><"+inputs.get(j).end);
  56. if(inputs.get(i).start>=inputs.get(j).end){
  57. ps[i] = j;
  58. }
  59. }
  60. }
  61. print_ps(ps);
  62. compute_opt(number_of_inputs-1,inputs,ps,opt);
  63. System.out.println("\nresult is: "+opt[number_of_inputs-1]);
  64.  
  65.  
  66. }
  67. public static void print_ps(int[] ps){
  68. for(int i=0;i<ps.length; i++) {
  69. System.out.print(ps[i]+" ");
  70. }
  71. }
  72. public static void print_inputs(ArrayList<SingleInputDP> inputs){
  73. for(int i=0;i<inputs.size();i++){
  74. SingleInputDP single_input = inputs.get(i);
  75. System.out.println(single_input.start+" "+single_input.end+" "+single_input.weight);
  76. }
  77.  
  78. }
  79.  
  80. public static int compute_opt(int j, ArrayList<SingleInputDP> inputs, int[] ps, int[] opt){
  81. opt[0] = 0;
  82. for(int i=1;i<inputs.size();i++){
  83. opt[i] = max(inputs.get(j).weight+opt[ps[i]],opt[i-1]);
  84. System.out.print(opt[i]+" ");
  85. }
  86. // if(j==-1){
  87. // return 0;
  88. // }
  89. // else{
  90. // return max(inputs.get(j).weight+compute_opt(ps[j], inputs, ps),compute_opt(j-1,inputs,ps));
  91. // }
  92. return 0;
  93. }
  94.  
  95. public static int max(int i, int j){
  96. if(i>=j){
  97. return i;
  98. }
  99. else{
  100. return j;
  101. }
  102. }
  103.  
  104. }
  105. class SingleInputDP{
  106. int start;
  107. int end;
  108. int weight;
  109. public SingleInputDP(int start, int end, int weight) {
  110. this.start = start;
  111. this.end = end;
  112. this.weight = weight;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement