Description
If you play games, you will know the importance of your equipment. However, your equipment is not the only thing to determine your success. Therefore, there are a lot of people winning without precious equipment because of their high fighting strength. Now, you are given a list of people's fighting strength(a1, a2, a3, ……, an). And the list is sorted according to their equipment value from low to high. Your target is to find how many such pairs(i, j) exists where ai > aj which means people i with lower equipment but higher fighting strength than people j.
Input
The first line is just an integer n (1 < n < 1000000).
The second line are n integers a1, a2, a3……an(|ai| < 2^31)
Output
You just need to output the number of pairs described above .
Sample Input
Sample Output
#include <iostream>
using namespace std;
long long ans;
int b[1000100];
void Sort(int a[], int first, int last)
{
if (first < last)
{
int mid = (first + last) / 2;
Sort(a,first, mid);
Sort(a,mid + 1, last);
//merge(a, first, mid, last);
int i = first, j = mid + 1, k = 0;
while (i <= mid&&j <= last) {
if (a[i] <= a[j])
b[++k] = a[i++];
else {
ans += mid - i + 1;
b[++k] = a[j++];
}
}
while (i <= mid) b[++k] = a[i++];
while (j <= last) b[++k] = a[j++];
for (i = first; i <= last; i++) a[i] = b[i - first + 1];
}
return;
}
int main()
{
int n;
int a[1000100];
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
ans = 0;
Sort(a,1, n);
cout << ans << endl;
return 0;
}
/**************************************************************
Problem: 1129
User: 141220110
Language: C++
Result: Accepted
Time:760 ms
Memory:9024 kb
****************************************************************/