fork download
  1. // The following implementation assumes that the activities
  2. // are already sorted according to their finish time
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class ActivitySelection
  8. {
  9. // Prints a maximum set of activities that can be done by a single
  10. // person, one at a time.
  11. // n --> Total number of activities
  12. // s[] --> An array that contains start time of all activities
  13. // f[] --> An array that contains finish time of all activities
  14. public static void printMaxActivities(int s[], int f[], int n)
  15. {
  16. int i, j;
  17.  
  18. System.out.print("Following activities are selected : n");
  19.  
  20. // The first activity always gets selected
  21. i = 0;
  22. System.out.print(i+" ");
  23.  
  24. // Consider rest of the activities
  25. for (j = 1; j < n; j++)
  26. {
  27. // If this activity has start time greater than or
  28. // equal to the finish time of previously selected
  29. // activity, then select it
  30. if (s[j] >= f[i])
  31. {
  32. System.out.print(j+" ");
  33. i = j;
  34. }
  35. }
  36. }
  37.  
  38. // driver program to test above function
  39. public static void main(String[] args)
  40. {
  41. int s[] = {1, 3, 0, 5, 8, 5};
  42. int f[] = {2, 4, 6, 7, 9, 9};
  43. int n = s.length;
  44.  
  45. printMaxActivities(s, f, n);
  46. }
  47.  
  48. }
  49.  
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
Following activities are selected : n0 1 3 4