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