HDU-2492-Ping pong(数状数组+简单思维)

Ping pong

Problem 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

解题思路:

看了好几篇博客都写的模模糊糊,看不太懂。
首先读懂题意,要找裁判打比赛。而距离不超过两个选手的距离,也就是说,裁判是位于两个选手之间的。然后裁判的分也必须介于两个选手之间。只要选手不同或者裁判不同就认为是不同的比赛。
那么问题就转化成。对于a【i】,超出左边有多少个数小于等于a【i】,右边有多少个数小于等于a【i】。a【i】位置上的答案就是左边小于或等于的个数乘右边大于等于的个数,加上左边大于等于的个数乘上右边小于等于的个数,注意还要减去,左边等于的个数乘上右边等于的个数,因为他们乘了两遍。
那么如果暴力去找左右小于a[i]的数是n2的那么必然超时。
此时可以想到用树状数组来快速的计算左右小于a[i]的个数。代价是log n的显然可以接受。
先初始化数组,然后从左往右添加元素,可以统计左边的。
再初始化数组,然后从右往左添加元素,可以统计右边的。
然后再遍历一遍保存的结果,计算最后答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
typedef long long ll;
const int MAX=101;
const int N= 1e6+10;
ll bits[N]; // 树状数组
ll a[N];
ll left1[N]; // 记录左边小于a[i]的个数
ll right1[N];// 记录右边小于a[i]的个数
ll l_e[N];   // 记录左边等于a[i]的个数
ll r_e[N];	 // 记录右边等于a[i]的个数

using namespace std;

ll lowbit(int x)
{
    return x&(-x);
}

void update(int x,int n)
{
    while(x<=n)
    {
        bits[x] ++;
        x += lowbit(x);
    }
}

ll sum(ll x)
{
    ll res = 0;
    while(x)
    {
        res += bits[x];
        x -= lowbit(x);
    }
    return res;
}

int main()
{
//    test();
//    return 0;
    ll t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(bits,0,sizeof(bits));
        ll ma = 0;
        for(int i = 0 ; i < n ; i ++)
        {
            cin>>a[i];
            ma = max(a[i],ma);
        }
        for(int i = 0 ; i < n ; i ++)
        {
            left1[i] = sum(a[i]-1);
            l_e[i] = sum(a[i]) - left1[i];
            update(a[i],ma+1);
        }
        memset(bits,0,sizeof(bits));
        for(int i = n-1 ; i >= 0  ; i --)
        {
            right1[i] = sum(a[i]-1);
            r_e[i] = sum(a[i]) - right1[i];
            update(a[i],ma+1);
        }
        ll  ans = 0;
        for(int i = 1; i < n-1; i ++)
        {
            ans += (left1[i]+l_e[i])*((n-1-i)-right1[i]) + (i-left1[i])*(right1[i]+r_e[i]);
            ans -= l_e[i]*r_e[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值