UVALive - 4329
Time Limit: | Memory Limit: | 64bit IO Format: |
Description
![Download as PDF Download as PDF](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
N N
20000)
Input
The first line of the input contains an integer T
20)
Every test case consists of ai
100000
Output
For each test case, output a single line contains an integer, the total number of different games.
Sample Input
1 3 1 2 3
Sample Output
1
解题报告
http://vjudge.net/problem/viewProblem.action?id=13895
大白书 197页
lmin[i]求i左边比i小的数的个数
#include<stdio.h>
#include<string.h>
#include<iostream>
#define lowbit(x) (x &(-x))
using namespace std;
const int M = 100005;
const int N = 20005;
int c[M],a[N],lmin[N],rmin[N];
void add (int x)
{
while (x <= M)
c[x] += 1,x += lowbit(x);
}
int sum(int x)
{
int res = 0;
while (x > 0)
res += c[x],x -= lowbit(x);
return res;
}
int main ()
{
int T,n,i;
scanf ("%d",&T);
while (T--)
{
scanf ("%d",&n);
memset (c,0,sizeof(c));
for (i = 1; i <= n; i ++)
{
scanf ("%d",&a[i]);
add(a[i]);
lmin[i] = sum(a[i]-1);
}
for( i=1;i<=n;i++)
cout<<lmin[i]<<'*';
memset (c,0,sizeof(c));
for (i = n; i >= 1; i --)
{
add(a[i]);
rmin[i] = sum(a[i]-1);
}
long long ans = 0;
for (i = 1; i <= n; i ++)
ans += lmin[i]*(n-i-rmin[i])+(i-lmin[i]-1)*rmin[i];
printf ("%lld\n",ans);
}
return 0;
}