1. 程式人生 > >A - Ping pong(樹狀陣列+順序對)

A - Ping pong(樹狀陣列+順序對)

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 81   Accepted Submission(s) : 27
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個乒乓球選手,住在一條直線上,從左到右依次輸入。每個選手有一個技術等級值。他們想打比賽,就要找裁判,為了省時和公正,選裁判的條件為裁判的位置和技術等級都在兩位選手之間,求能打多少場比賽。

思路:

雖然不是第一次接觸樹狀陣列,但是做到這個題的時候,第一時間也還是想不通怎麼把樹狀陣列用上的,一開始用的暴力,肯定超時了,後來請教大神,終於領悟了一點點,就是不用仔細去研究樹狀陣列是怎樣構成的,就把他看作可以求區間和的一個東西就行。

總的思路就是對於除了最左邊和最右邊兩個數之外,對於之間這部分數求每個數左邊小於它的數的個數minl[i]乘以右邊大於它的數的個數加上每個數左邊大於它的個數乘以右邊小於它的個數minr[i],然後全部求和。核心在於怎樣求minl[i]和minr[i]兩個陣列。

先將C陣列初始化為0,將輸入的每個數值作為下標帶進樹狀陣列,求1-a[i]的區間和(minl[i]),即1的數量就是a[i]左邊小於他的數的個數,每求完一個區間和就在這個位置上加1,表示這個位置有數了,然後按照這個規律迴圈一遍就可以把所有數的左邊小於它的個數給求出來,然後在反過來遍歷一遍,就可以把每個數右邊小於它的數的個數給求出來(minr[i])。

比如說 數列為:5 4 3 6 8

minl[1]=sum[5];  sum[5]=0+0+0+0+0=0;

然後5的位置上加一。此時c陣列為:0 0 0 0 1 0

minl[2]=sum[4]; sum[4]=0+0+0+0=0;

然後4的位置上加一。此時c陣列為:0 0 0 1 1 0

minl[3]=sum[3];sum[3]=0+0+0=0;

然後4的位置上加一。此時c陣列為:0 0 1 1 1 0

minl[4]=sum[6]; sum[6]=0+0+1+1+1+0=3;

然後6的位置上加一。此時c陣列為:0 0 1 1 1 1

minl[5]=sum[8]; sum[8]=0+0+1+1+1+1+0+0=4;

然後8的位置上加一。此時c陣列為:0 0 1 1 1 1 0 0 1

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 2e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
const int MAXN=100005;

int a[MAXN],c[MAXN],minr[MAXN],minl[MAXN];

int lowbit(int i)
{
    return i&(-i);
}

void update(int pos,int num)
{
    while(pos<MAXN)
    {
        c[pos]+=num;
        pos+=lowbit(pos);
    }
}

int sum(int pos)
{
    int s=0;
    while(pos>0)
    {
        s+=c[pos];
        pos-=lowbit(pos);
    }
    return s;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            minl[i]=sum(a[i]);
            update(a[i],1);
        }
        memset(c,0,sizeof(c));
        for(int i=n;i>=1;i--)
        {
            minr[i]=sum(a[i]);
            update(a[i],1);
        }
        ll res=0;
        for(int i=1;i<=n;i++)
        {
            res+=minl[i]*(n-i-minr[i])+minr[i]*(i-1-minl[i]);
        }
        cout<<res<<endl;
    }
    return 0;
}