HDU 2492 PingPong (树状数组)

来源:互联网 发布:学术讲座海报 大数据 编辑:程序博客网 时间:2024/06/07 09:10

问题描述:
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

大致题意:

有N个队员,每个人的战斗力给出。要进行一场比赛,需要一个裁判,裁判的战斗力必须处于两个人中间。问能组成几场比赛。裁判不同,或者比赛的队员有一个不同就认为是不同的比赛。

思路分析:

树状数组。
我一开始想的是。枚举裁判,找出有多少比他大的有多少比他小的。
想乘就是答案了。
不过写了一发wa了。没有找到原因。
后来看了看题解。思路是没问题的。
我们可以求出在这个数,左边有几个比他小的。那么左边的总数(i-1)减去比他小的就是比他大的。i-b[i]-1。
倒着来一遍求出右边有多少比他小的。同理,比他大的就是n-i-c[i]。
那么答案就是:b[i]*(n-i-c[i])+(i-b[i]-1)*c[i]。
挺好的一个题。
这个可以不用离散化,如数据过大,可以使用离散化来优化程序。

在下面附上我wa的代码,希望有大神找到错误,可以指出来。

wa的代码:

#include<bits/stdc++.h>using namespace std;#define N 100000int e[2*N+5];int n,m,maxx,T;struct Node{    int num;    int NUM;    int Max,Min;    void get_NUM()    {        NUM=maxx+1-num;    }}a[N+5];bool com1(Node a,Node b){    return a.num<b.num;}bool com2(Node a,Node b){    return (maxx+1-a.num)<(maxx+1-b.num);}int lowbit(int x){    return x&(-x);}int get_sum(int x){    int sum=0;    for(;x>0;x-=lowbit(x))    {        sum+=e[x];    }    return sum;}void update(int x,int v){    for(;x<=maxx;x+=lowbit(x))    {        e[x]+=v;    }}int main(){    ios::sync_with_stdio(false);    cin>>T;    while(T--)    {        int i,j,k;        cin>>n;        for(i=0;i<n;i++)        {            cin>>a[i].num;            maxx=max(maxx,a[i].num);        }        sort(a,a+n,com1);        memset(e,0,sizeof(e));        for(i=0;i<n;i++)        {            update(a[i].num,1);            a[i].Min=get_sum(a[i].num-1);        }        memset(e,0,sizeof(e));        sort(a,a+n,com2);        for(i=0;i<n;i++)        {            a[i].get_NUM();            update(a[i].NUM,1);            a[i].Max=get_sum(a[i].NUM-1);        }        long long sum=0;        for(i=0;i<n;i++)        {            sum+=a[i].Max*a[i].Min;        }        cout<<sum<<endl;    }    return 0;}

ac代码:

#include <cstdio>#include <algorithm>using namespace std;int sum[100001],a[20001],b[20001],c[20001],n,mn;int lowbit(int x){    return x&-x;}int query(int x){    int ans=0;    while(x>=1)    {        ans+=sum[x];        x-=lowbit(x);    }    return ans;}void add(int x){    while(x<=mn)    {        sum[x]++;        x+=lowbit(x);    }}int main(){    int T,i;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        mn=0;        for(i=1; i<=n; i++)        {            scanf("%d",&a[i]);            mn=max(a[i],mn);        }        for(i=0; i<=mn; i++) sum[i]=0;        for(i=1; i<=n; i++)        {            add(a[i]);            b[i]=query(a[i]-1);//求出第i个人的前面rank比他小的人数,存入数组b中        }        for(i=0; i<=mn; i++) sum[i]=0;        for(i=n; i>=1; i--)        {            add(a[i]);            c[i]=query(a[i]-1);//求出第i个人的后面rank比他小的人数,存入数组c中        }        long long ans=0;        for(i=2; i<n; i++)        {            ans+=b[i]*(n-i-c[i])+(i-b[i]-1)*c[i];  //左边小的乘上右边大的加上,左边大的乘上右边小的。        }        printf("%I64d\n",ans);    }}
0 0
原创粉丝点击