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[] a = {1, 10, 2, 5, 10, 1};
  14.  
  15. int n = a.length;
  16. int[] dp1 = new int[n];
  17. int[] dp2 = new int[n];
  18.  
  19.  
  20. for(int i = 0; i < n; i++){
  21. dp1[i] = 1;
  22. for(int j = i - 1; j >= 0; j--){
  23. if(a[j] < a[i]){
  24. dp1[i] = dp1[i] + dp1[j];
  25. }
  26. }
  27. }
  28.  
  29. for(int i = n-1; i >= 0; i--){
  30. dp2[i] = 1;
  31. for(int j = i + 1; j < n; j++){
  32. if(a[j] < a[i]){
  33. dp2[i] = dp2[i] + dp2[j];
  34. }
  35. }
  36. }
  37.  
  38. int count = 0;
  39. for(int i = 0; i < n; i++){
  40. count += (dp1[i] - 1 ) * (dp2[i] - 1);
  41. }
  42.  
  43. System.out.println(count);
  44.  
  45. }
  46. }
Success #stdin #stdout 0.07s 54628KB
stdin
Standard input is empty
stdout
16