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. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15. int a[]=new int[n+1];
  16. for(int i=1;i<=n;i++)
  17. a[i]=sc.nextInt();
  18.  
  19. int suffix[]=new int[n+1];
  20. suffix[n]=1;
  21. for(int i=n-1;i>=1;i--)
  22. {
  23. if(a[i]<a[i+1])
  24. suffix[i]=suffix[i+1]+1;
  25. else
  26. suffix[i]=1;
  27. }
  28.  
  29. int prefix=0;
  30. int val=0;
  31. int sum=0;
  32. for(int i=1;i<=n;i++)
  33. {
  34. if(prefix>a[i])
  35. val++;
  36. else
  37. val=1;
  38.  
  39. sum+= (val-1)*(suffix[i]-1);
  40. prefix=a[i];
  41. }
  42. System.out.println(sum);
  43.  
  44. }
  45. }
Success #stdin #stdout 0.11s 54536KB
stdin
4
5 3 4 8
stdout
2