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. // storing pairs a[i]>a[j];
  20. int pref[]=new int[n+1];
  21. pref[1]=0;
  22.  
  23. for(int j=2;j<=n;j++)
  24. {
  25. int c=0;
  26. for(int i=1;i<j;i++)
  27. {
  28. if(a[i]>a[j])
  29. c++;
  30. }
  31. pref[j]=c;
  32. }
  33.  
  34.  
  35. //storing pairs a[k]>a[l]
  36. int suff[]=new int[n+1];
  37.  
  38. suff[n]=0;
  39.  
  40. for(int k=n-1;k>=1;k--)
  41. {
  42. int c=0;
  43. for(int l=k+1;l<=n;l++)
  44. {
  45. if(a[k]>a[l])
  46. c++;
  47. }
  48. suff[k]=c;
  49. }
  50.  
  51. int ans=0;
  52. for(int j=1;j<=n;j++)
  53. {
  54. for(int k=j+1;k<=n;k++)
  55. {
  56. if(a[j]<a[k])
  57. ans+=pref[j]*suff[k];
  58. }
  59. }
  60.  
  61. System.out.println(ans);
  62. }
  63. }
Success #stdin #stdout 0.14s 56552KB
stdin
6
1 2 1 5 1 34 35
stdout
1