hdu 2492

来源:互联网 发布:如何提高睡眠 知乎 编辑:程序博客网 时间:2024/05/29 14:48

题意:有一群乒乓球爱好者住在一条街上,他们的位置是依次从西往东。他们之间经常比赛,每次比赛需要一个裁判,裁判的能力必须在他们的能力之间且要住在他们之间。每个球员的能力都不一样,问:这条街能有多少种不同比赛(选手或裁判不一样)。

球员的能力依次为A1-----An。那么若选Ai作为裁判,在1---------i - 1之间有Ci个人的能力Ai小,那么有I - 1 - Ci个人的能力比Ci大。同理:在i + 1 ----------------n 之间有di个人的能力比Ai小,那么有n - i - 1 - di个人比Ai强。那么选Ai做裁判,有Ci * (n - i - 1 - di) + di * (i - 1 - CI)  种比赛。

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn = 20000 + 5;int t_mi[maxn], t_ma[maxn], player[maxn];struct node{    int value;    int p;    bool operator < (const node& a) const    {        return value < a.value;    }}pp[maxn];void ADD_BIT(int x, int n){    while(x <= n)    {        t_mi[x] += 1;        x += x & -x;    }}void BIT_ADD(int x, int n){    while(x <= n)    {        t_ma[x] += 1;        x += x & -x;    }}void BIT_REMOVE(int x, int n){    while(x <= n)    {        t_ma[x] -= 1;        x += x & -x;    }}int GET_SUM(int n, int *p){    int ret = 0;    while(n > 0)    {        ret += p[n];        n -= n &- n;    }    return ret;}int main(){    int T;    scanf("%d", &T);    for(int kase = 1; kase <= T; kase++)    {        int n;        scanf("%d", &n);        for(int i = 0; i < n; i++)        {            scanf("%d", &pp[i].value);            pp[i].p = i;        }        sort(pp, pp + n);        for(int i = 0; i < n; i++)        {            player[pp[i].p] = i + 1;        }        //for(int i = 0; i < n; i++)//测试离散            //printf("%d\n", player[i]);        for(int i = 0; i <= n; i++)            t_mi[i] = t_ma[i] = 0;        for(int i = 2; i < n; i++)            BIT_ADD(player[i], n);        ADD_BIT(player[0], n);        long long ans = 0;        for(int i = 1; i < n - 1; i++)        {            int c = GET_SUM(player[i], t_mi);            int d = GET_SUM(player[i], t_ma);            //printf("%d %d\n", c, d);            ans = ans + (i - c) * d + c * (n - i - d - 1);            ADD_BIT(player[i], n);            BIT_REMOVE(player[i + 1], n);        }        printf("%lld\n", ans);    }    return 0;}