There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample Input
Sample test(s)
Input
5
2 3 1 5 4
2 3 1 5 4
Output
3
- #include"iostream"
- #include"algorithm"
- #include"cstring"
- #include"cstdio"
- using namespace std;
- #define max 65550
- structab
- {
- int value;
- int index;
- }a[max];
- /*bool cmp(const ab &a,const ab &b)
- {
- if(a.value!=b.value)
- return a.value<b.value;
- else
- return a.index<b.index;
- }*/
- bool cmp(const ab&a,const ab&b)
- {
- return a.value<b.value;
- }
- int c[max],b[max];
- int n;
- int lowbit(int x)
- {
- return x&(-x);
- }
- void updata(int x,int d)
- {
- while(x<=n)
- {
- c[x]+=d;
- x+=lowbit(x);
- }
- }
- int getsum(int x)
- {
- int res=0;
- while(x>0)
- {
- res+=c[x];
- x-=lowbit(x);
- }
- return res;
- }
- int main()
- {
- int i;
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- {
- scanf("%d",&a[i].value);
- a[i].index=i;
- }
- sort(a+1,a+1+n,cmp);
- b[a[1].index]=1;
- memset(c,0,sizeof(c));
- //memset(b,0,sizeof(b));
- for(i=2;i<=n;i++)
- {
- if(a[i].value!=a[i-1].value)
- b[a[i].index]=i;
- else
- b[a[i].index]=b[a[i-1].index];
- }
- long long int sum=0;
- for(i=1;i<=n;i++)
- {
- updata(b[i],1);
- sum+=getsum(n)-getsum(b[i]);
- }
- printf("%lld\n",sum);
- return 0;
- }