分析一下,感觉没什么思路,再想一下,结果不就是每一位的数小于它的数乘以大于大于这位数的相乘之和吗,我们可以利用逆序对的思维求得
关键点在于求解逆序对的时候值相同的时候,位置大的优先级更高处理
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 3e4 + 5;
struct node
{
int va, pos1, pos2;
int mi, ma;
}sto[N];
bool cmp1(node a, node b) {
if (a.va < b.va) return 1;
return a.va == b.va ? a.pos1 > b.pos1 : 0;
}
bool cmp2(node a, node b) {
if (a.va > b.va) return 1;
return a.va == b.va ? a.pos2 > b.pos2 : 0;
}
int a[N];
int b[N];
int c[N];
int n;
int lowbit(int x) { return x & (-x); }
void add(int x, int p) {
for (int i = x; i <= n; i += lowbit(i)) {
a[i] += p;
}
}
int find(int x) {
int ans = 0;
for (int i = x; i; i -= lowbit(i)) ans += a[i];
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> sto[i].va;
sto[i].pos1 = i;
sto[i].pos2 = n - i + 1;
}
sort(sto + 1, sto + 1 + n, cmp1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
int t = sto[i].pos1;
b[sto[i].pos1] = find(t - 1);
//cout << sto[i].pos1 << " " << sto[i].mi << endl;
add(t, 1);
}
memset(a, 0, sizeof a);
sort(sto + 1, sto + 1 + n, cmp2);
for (int i = 1; i <= n; i++) {
int t = sto[i].pos2;
c[sto[i].pos1] = find(t - 1);
add(t, 1);
}
for (int i = 1; i <= n; i++) {
ans += b[i] * c[i];
//cout << " " << b[i] << " " << c[i] << endl;
}
cout << ans;
return 0;
}