POJ 3928-Ping pong(树状数组+加/乘法原理)

Ping pong
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2982 Accepted: 1103

Description

N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee's house. For some reason, the contestants can't choose a referee whose skill rank is higher or lower than both of theirs. The contestants have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?

Input

The first line of the input contains an integer T(1<=T<=20), indicating the number of test cases, followed by T lines each of which describes a test case. 
Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, a2 ... aN follow, indicating the skill rank of each player, in the order of west to east. (1 <= ai <= 100000, i = 1 ... N).

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

Source

题目意思:

一条街上有n个乒乓球爱好者,每个人都有一个不同的技能值 a 。每场比赛需要选3个人:1个裁判,2个选手,有一个奇怪的规定:裁判必须住在选手中间,且裁判的技能值也要在选手中间。求能有多少种比赛。

解题思路:(来源已未可考)

枚举第 i 个人当裁判,设 a0 到 ai-1 中,有 bi 个数比 ai 小,ai+1 到 an-1中有 di 个数比 ai 大,那么比赛种数为 b[i]*(n-i-1-d[i])+(i-b[i])*d[i].

要求 bi 和 di ,可以采用树状数组。ai 的最大值为100000,那么BIT的范围是1-100000,A[1,100000]每个数 A[i] 代表一个人,一开始BIT中的每个位置上的数都是0,表示这个位置上没有人,C[] 表示BIT中的辅助数组,初始化为0。每当插入一个 ai ,相当于把值为 ai 的人放在BIT中的相应位置上,即对应位置上的值要加1,从初始的0变为1,此时需要修改相应的C[] 中的值。

由于我要求的是比 ai 小的数的个数,那么这些比 ai 小的数在BIT中的位置必然是在 ai 之前,也就是这些位置上的值为1,那么 A[ai] 的前缀和就等于这些数的个数。

当要求 ai+1 到 an-1 中比 ai 小的数时,把数组 a[] 反过来插入BIT中即可。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <vector>
#include <cstdlib>
#define maxn 100010
#define ll long long
using namespace std;
int a[maxn],c[maxn];
int mins[maxn],maxs[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int d)
{
    while(x<=maxn)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
}

int sum(int i)
{
    int res=0;
    while(i>0)
    {
        res+=c[i];
        i-=lowbit(i);
    }
    return res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        memset(c,0,sizeof(c));
        int n;
        cin>>n;
        for(int i=1; i<=n; ++i)
            cin>>a[i];
        for(int i=1; i<=n; ++i)
        {
            mins[i]=sum(a[i]);//计算比a[i]小的数的个数
            update(a[i],1);//放进一个a[i]
        }
        memset(c,0,sizeof(c));
        for(int i=n; i>0; --i)
        {
            maxs[i]=sum(a[i]);
            update(a[i],1);
        }
        ll ans=0;
        for(int i=1; i<=n; i++)
            ans+=(mins[i]*(n-i-maxs[i])+(i-1-mins[i])*maxs[i]);
        cout<<ans<<endl;
    }
    return 0;
}
/**
1
3
1 2 3
**/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值