hdu 2492 Ping pong(线段树+思维)

Ping pong

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


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
 

Source
2008 Asia Regional Beijing
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2485  2491  2490  2489  2488 
  题意:
给你长度为N(3<=N<=20000) 的数列A。数列中每个数字互不相同。问你有多少满足条件的i,j,k满足。
A[i]<=A[j]<=A[k]。且i<j<k。1 <= A[i] <= 100000,
思路:
由于时限是1s所以暴力的方法是要超时的。 要求解这个问题。我如果知道了A[i]前面有多少个数比它小。后面有多少个数比它大。分别记为dpl[i],dpr[i]。那么我们就可以知道以i作为中间值的组合数。
ans=dpl[i]*dpr[i]+(i-1-dpl[i])*(n-i-dpr[i])。所以我们只需用线段树求出dpl,和dpr然后线性扫描一次A就可以得到答案了。时间复杂度O(n*log2(max(A[i])))完全够用。但是要注意答案可能会超int所以用long long 保存。
详细见代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=100010;
int sum[maxn<<2],dpl[maxn],dpr[maxn],val[maxn];
void update(int L,int R,int p,int k)
{
    int ls,rs,mid;
    if(L==R)
    {
        sum[k]=1;
        return ;
    }
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(p<=mid)
        update(L,mid,p,ls);
    else
        update(mid+1,R,p,rs);
    sum[k]=sum[ls]+sum[rs];
}
int qu(int L,int R,int l,int r,int k)
{
    int ls,rs,mid;
    if(l==L&&r==R)
        return sum[k];
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(l>mid)
        return qu(mid+1,R,l,r,rs);
    else if(r<=mid)
        return qu(L,mid,l,r,ls);
    else
        return qu(L,mid,l,mid,ls)+qu(mid+1,R,mid+1,r,rs);
}
int main()
{
    int t,n,i,lim;

    scanf("%d",&t);
    while(t--)
    {
        lim=0;memset(sum,0,sizeof sum);
        scanf("%d",&n);
        memset(sum,0,sizeof sum);
        for(i=0;i<n;i++)
            scanf("%d",&val[i]),lim=max(lim,val[i]);
        for(i=0;i<n;i++)
        {
            dpl[i]=qu(1,lim,1,val[i],1);
            update(1,lim,val[i],1);
        }
        memset(sum,0,sizeof sum);
        for(i=n-1;i>=0;i--)
        {
            dpr[i]=qu(1,lim,val[i],lim,1);
            update(1,lim,val[i],1);
        }
        long long ans=0;
        for(i=0;i<n;i++) ans+=dpl[i]*dpr[i]+(i-dpl[i])*(n-i-1-dpr[i]);
        cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)