fork download
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. int n = scanner.nextInt();
  7.  
  8. int[] b = new int[n + 1];
  9. int[] left = new int[n + 1];
  10. int[] right = new int[n + 1];
  11. int[] valley = new int[n + 1];
  12.  
  13. int sum = 0;
  14.  
  15. // Input array b
  16. for (int i = 1; i <= n; i++) {
  17. b[i] = scanner.nextInt();
  18. }
  19. // set 0 as greater for left and right at first..
  20. left[1] = 0;
  21. for(int i = 2; i <= n; i++){
  22. if(b[i] < b[i-1]){
  23. left[i] = left[i-1] + 1;
  24. }
  25. else{
  26. left[i] = 0;
  27. }
  28. }
  29. right[n] = 0;
  30. for(int i = n - 1; i >= 1; i--){
  31. if(b[i] < b[i+1]){
  32. right[i] = right[i+1] + 1;
  33. }
  34. else{
  35. right[i] = 0;
  36. }
  37. }
  38.  
  39. for(int i = 1; i <= n; i++){
  40. valley[i] = right[i] * left[i];
  41. sum += valley[i];
  42. }
  43.  
  44. System.out.println(sum);
  45.  
  46. scanner.close();
  47. }
  48. }
  49.  
Success #stdin #stdout 0.11s 54452KB
stdin
4
5 3 4 8
stdout
2