求可以举行多少场比赛 要求每场比赛3个人 裁判在中 技能值要在2个选手中间 位置(下标)也要在2个人中间
对于 ai 如果从1到i-1 有x个数小于ai (有I-1-x比ai大)从i+1到n有y个比ai小(有i-n-y个比ai大)那么对于ai 符合的有x*(i-n-y)+(i-1-x)*y
求x,y可以从头和从尾扫描2边用树状数组统计 计数排序那样对于ai cnt[a[i]] = 1
#include <cstdio> #include <cstring> using namespace std; const int maxn = 20010; int a[maxn]; int l[maxn*5]; int r[maxn*5]; int cnt[maxn*5]; int n; int lowbit(int x) { return x & -x; } int sum(int x) { int ret = 0; while(x > 0) { ret += cnt[x]; x -= lowbit(x); } return ret; } void add(int x) { while(x <= 100010) { cnt[x]++; x += lowbit(x); } } int main() { int T; int i; scanf("%d", &T); while(T--) { scanf("%d", &n); for(i = 1; i <= n; i++) scanf("%d", &a[i]); memset(l, 0, sizeof(l)); memset(r, 0 ,sizeof(r)); memset(cnt, 0 ,sizeof(cnt)); for(i = 1; i <= n; i++) { l[i] = sum(a[i]); add(a[i]); //printf("%d\n",l[i]); } memset(cnt, 0 ,sizeof(cnt)); for(i = n; i >= 1; i--) { r[i] = sum(a[i]); add(a[i]); } long long s = 0; for(i = 2; i < n; i++) { s += (long long)l[i]*(n-i-r[i]) + (long long)(i-1-l[i])*r[i]; } printf("%lld\n", s); } return 0; }