admin管理员组文章数量:1567541
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
53 2 1 4 5Sample Output
3#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
****************************************************************/
本文标签: Fightingstrengthequipment
版权声明:本文标题:Fighting strength vs Equipment 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1726266804a1063328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论