HDU 2492 Ping pong

题意:

一串数字  问  有几种这样的组合(x,y,z)使得x>y>z或x<y<z  y在x数字后面z在y后面  题目中每种数字是唯一的

思路:

对于一个数字  比如 f  它计算出的ans值为

( beforef.lessthanf * afterf.biggerthanf )+( beforef.biggerthanf * afterf.lessthanf )

易知 beforef.biggerthanf = locationf - 1 - beforef.lessthanf 同理after

则式子可化简为

( beforef.lessthanf * ( n - locationf - afterf.lessthanf ) )+( (
locationf - 1 - beforef.lessthanf ) * afterf.lessthanf )

如果我们从小到大的处理每个数字  那么可知当前处理的数字是当前序列中最大的

则 afterf.lessthanf = nowallnumber - beforef.lessthanf

那么式子中将只出现一个未知数  也就是说我们只需要知道当前位置前有几个小于自己即可

因此用树状数组维护

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 20010
#define lowbit(x) (x&(-x))

int t,n;
int a[N];
__int64 ans;
struct person
{
    int id,val;
    bool operator<(const person fa) const
    {
        return val<fa.val;
    }
}f[N];

void add(int x)
{
    while(x<=n)
    {
        a[x]+=1;
        x+=lowbit(x);
    }
}

int sum(int x)
{
    int res=0;
    while(x)
    {
        res+=a[x];
        x-=lowbit(x);
    }
    return res;
}

int main()
{
    int i,ls,lb,rs,rb,id;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            f[i].id=i;
            scanf("%d",&f[i].val);
        }
        sort(f+1,f+1+n);
        memset(a,0,sizeof(a));
        ans=0;
        for(i=1;i<=n;i++)
        {
            id=f[i].id;
            ls=sum(id);
            lb=id-ls-1;
            rs=sum(n)-ls;
            rb=n-id-rs;
            ans+=ls*rb;
            ans+=lb*rs;
            add(id);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

HDU 2492 Ping pong

时间: 2024-10-09 06:50:51

HDU 2492 Ping pong的相关文章

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong 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

hdu 2492 Ping pong 线段树

给定一个序列,求出一共有多少个三元组(ai,aj,ak),使得i<j<k,ai<aj<ak. 固定中间值,查找前面比他大的有多少,比他小的有多少,查找后面比他大的有多少,比他小的有多少. #include <stdio.h> #include <string.h> #define maxn 100200 #define N 20100 int sum[maxn*4]; int lmax[N],lmin[N],rmax[N],rmin[N]; int ans[

POJ 3928 &amp; HDU 2492 Ping pong(树状数组求逆序数)

题目链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 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 im

POJ 3928 &amp;amp; HDU 2492 Ping pong(树阵评价倒数)

主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 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 im

【HDOJ】2492 Ping pong

线段树+离散化. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 20005 6 #define lson l, mid, rt<<1 7 #define rson mid+1, r, rt<<1|1 8 9 int buf[MAXN], bk[MAXN]; 10 int sum[MAXN<<2], n; 11 12 int

hdu 2492(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4011    Accepted Submission(s): 1482 Problem Description N(3<=N<=20000) ping pong

POJ 3928 &amp; hdu 2492 &amp; Uva1428 PingPong 【树状数组】

Ping pong                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   链接:hdu 2492 Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider

树状数组+逆序数与顺序数——HDU 2492

对应HDU题目:点击打开链接 Ping pong Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description N(3N20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skil

Ping pong

Description N(3N20000) 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