hdu 2492 Ping pong(树状数组)

来源:互联网 发布:学51单片机可以做什么 编辑:程序博客网 时间:2024/04/29 03:11

Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4870    Accepted Submission(s): 1776


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
13 1 2 3
 

Sample Output
1
 


从n个数中选3个数 是的中间位置的一个数的大小也是三个数中的中间值 问有多少种情况

使用树状数组来求插入一个数时前面已经插入的比该数小的数的个数。。。

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 100010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int a[MAXN],c[MAXN],x1[MAXN],x2[MAXN],yy[MAXN],y2[MAXN];int lowbit(int n){    return n&(-n);}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(){//    fread;    int tc;    scanf("%d",&tc);    while(tc--)    {        int n;        scanf("%d",&n);        MEM(c,0);        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            int k=sum(a[i]);            x1[i]=k;//前面输入的i个数中有k个比a[i]小            x2[i]=i-k-1;//前面输入的i个数中有i-k-1个比a[i]大            update(a[i],1);//            cout<<i<<"  "<<x1[i]<<"  "<<x2[i]<<endl;        }        MEM(c,0);        int j=1;//当先输入数的个数        for(int i=n;i>=1;i--,j++)        {            int k=sum(a[i]);            yy[i]=k;//后面的j个数中有k个比a[i]小            y2[i]=j-1-k;            update(a[i],1);//            cout<<i<<"  "<<yy[i]<<"  "<<y2[i]<<endl;        }        ll ans=0;        for(int i=1;i<=n;i++)        {            ans+=x1[i]*y2[i]+x2[i]*yy[i];        }        printf("%I64d\n",ans);    }    return 0;}





0 0
原创粉丝点击