Inversionstime Limit per test:0.25 sec.
Memory limit per test:4096 Kbinput:standard
Output:standard
There is 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].
InputThe first line of the input contains the number N. The second line contains N numbers A1 ... An.
OutputWrite amount of such pairs.
Sample Test (s)
Input5
2 3 1) 5 4
Output3
This problem needs to be discrete, the tree-like array is discrete after the inverse, statistics added to the element when the current array
How many larger numbers already exist, which is the contribution of the number to the latter in reverse order, and then you can
Solved, the general need discretization.
1#include <cstdio>2#include <algorithm>3#include <cmath>4#include <cstring>5#include <iostream>6 #defineN 700077 using namespacestd;8 9 intN;Ten Long Longans; One intB[n],c[n]; A structNode - { - intZhi,id; the }a[n]; - - BOOLcmp (Node x,node y) - { + returnx.zhi<Y.zhi; - } + intLowbit (intx) A { at returnx& (-x); - } - voidChangeintXinty) - { - for(inti=x;i<=n;i+=lowbit (i)) -c[i]+=y; in } - intQueryintx) to { + intres=0; - for(inti=x;i>=1; i-=lowbit (i)) theres+=C[i]; * returnRes; $ }Panax Notoginseng intMain () - { thescanf"%d",&n); + for(intI=1; i<=n;i++) A { thescanf"%d",&A[i].zhi); +A[i].id=i; - } $Sort (A +1, a+n+1, CMP); $ intCnt=0; - for(intI=1; i<=n;i++) - { the if(a[i].zhi!=a[i-1].zhi) b[a[i].id]=++CNT; - Elseb[a[i].id]=CNT;Wuyi } the for(intI=1; i<=n;i++) - { WuChange (B[i),1); -Ans=ans+query (N)-query (B[i]); About } $printf"%lld", ans); -}
SGU180 (tree-like array, reverse order, discrete)