首页 > ACM题库 > HDU-杭电 > hdu 2492 Ping pong-线段树-[解题报告]C++
2014
01-26

hdu 2492 Ping pong-线段树-[解题报告]C++

Ping pong

问题描述 :

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?

输入:

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).

输出:

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).

样例输入:

1
3 1 2 3

样例输出:

1

给一列互不相同的数,统计第i个位置(2 < i < n-1)左侧比他值小的与右侧比他大的乘积+左侧比他大的与右侧比他小的乘积 的和。看懂题意后实际就是个bit的应用…不过需要稍微预处理下,用a[i],c[i]两个数组表示,更新到第i个数时,能力值小于等于当前位置能力值的人数和能力值大于当前位置能力值的人数。循环一边处理完后,从2到n-i枚举一边,把左边小的和右边大的乘起来,左边大的右边小的乘起来,求个和就像你给了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <memory.h>
using namespace std;
typedef __int64 ll;
const int maxn=200010;
int tr[maxn];
int n,m,p,q,k,mxn;
struct BIT
{
    int lowbit(int x)
    {
        return x&-x;
    }
    void add(int x,int c=1)
    {
        for (int i=x; i<=mxn; i+=lowbit(i))
        {
            tr[i]+=c;
        }
    }
    ll sum(int x)
    {
        ll res=0;
        for (int i=x; i>0; i-=lowbit(i))
        res+=tr[i];
        return res;
    }
}bit;
int tt;
ll a[maxn],c[maxn];
int dt[maxn];
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&tt);
    while(tt--)
    {
        memset(tr,0,sizeof tr);
        scanf("%d",&n);
        mxn=0;
        for (int i=1; i<=n; i++)
        {
            scanf("%d",&dt[i]);
            mxn=max(mxn,dt[i]);
        }
        for (int i=1; i<=n; i++)
        {
          m=dt[i];
          bit.add(m,1);
          a[i]=bit.sum(m);
          c[i]=ll(i-a[i]);
        }
//        for (int i=1; i<=n; i++)
//        cout<<a[i]<<" "<<c[i]<<endl;
        ll ans=0;
        ll x1,x2,y1,y2;
        for (int i=2; i<n; i++)
        {
            x1=a[i]-1;
            x2=bit.sum(dt[i])-a[i];
            y1=(i-a[i]);
            y2=n-bit.sum(dt[i])-y1;
//            cout<<x1<<" "<<y2<<" | "<<x1<<" "<<x2<<endl;
            ans+=x1*y2;
            ans+=y1*x2;
        }
        printf("%I64d\n",ans);

    }
    return 0;
}

解题转自:http://blog.csdn.net/night_raven/article/details/12356853


  1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。

  2. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥