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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int n = 6;
  14. int k = 2;
  15. int M = 2;
  16.  
  17. int[] arr = {1, 2, 3, 10, 11, 12};
  18.  
  19. int[] dp = new int[n + 1];
  20. Arrays.fill(dp, 100);
  21.  
  22. for(int i = 0; i < k - 1; i++) dp[i] = 0;
  23.  
  24. for(int i = 0; i < n; i++){
  25.  
  26. int low = arr[i];
  27. int high = arr[i];
  28. int j = i - k;
  29. int ans = 100;
  30.  
  31. while(j >= 0 && high - low <= M){
  32. ans = Math.min(ans, 1 + dp[j]);
  33. low = Math.min(low, arr[j]);
  34. high = Math.max(high, arr[j]);
  35. j--;
  36. }
  37. dp[i+1] = ans;
  38. }
  39.  
  40. System.out.println(dp[n]);
  41. }
  42. }
Success #stdin #stdout 0.1s 52568KB
stdin
Standard input is empty
stdout
2