POJ 3928 & 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 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

Beijing 2008

PS:

分别求每个数的左右两边比它大的个数和小的个数!最后再交叉相乘就可以!

代码例如以下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100017;
int n;
int a[maxn], c[maxn];
int leftMax[maxn], leftMin[maxn];
int rightMax[maxn], rightMin[maxn];
typedef __int64 LL;

int Lowbit(int x) //2^k
{
    return x&(-x);
}

void update(int i, int x)//i点增量为x
{
    while(i <= maxn)//注意此处
    {
        c[i] += x;
        i += Lowbit(i);
    }
}
int sum(int x)//区间求和 [1,x]
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=Lowbit(x);
    }
    return sum;
}

int main()
{
    int t;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
        }
        memset(c,0,sizeof(c));
        for(int i = 1; i <= n; i++)
        {
            leftMin[i] = sum(a[i]);//计算左边小的个数
            leftMax[i] = i-leftMin[i]-1;//计算左边大的个数
            update(a[i],1);
        }
        memset(c,0,sizeof(c));//再次清零
        for(int i = n,j = 1; i >= 1; i--,j++)
        {
            rightMin[i] = sum(a[i]);
            rightMax[i] = j-rightMin[i]-1;
            update(a[i],1);
        }
        LL ans = 0;
        for(int i = 1; i <= n; i++)
        {
            ans+=leftMax[i]*rightMin[i] + leftMin[i]*rightMax[i];//交叉相乘取和
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-11-08 03:13:34

POJ 3928 &amp; 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[

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

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

【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

poj3928 Ping pong 树状数组

http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2087   Accepted: 798 Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player

uva 1428 - Ping pong(树状数组)

题目链接:uva 1428 - Ping pong 题目大意:一条大街上住着n个乒乓球爱好者,经常组织比赛.每个人都有一个不同的能力值,每场比赛需要3个人,裁判要住在两个选手之间,并且能力值也要在选手之间,问说最多能举行多少场比赛. 解题思路:预处理出bi和ci分别表示说在1~i中能力值比第i个人小的人和i+1~n中能力值比第i个人小的.处理过程用树状数组维护即可. #include <cstdio> #include <cstring> #include <algorith

POJ-3928 Ping pong(树状数组)

Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2985   Accepted: 1104 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 i

POJ 3928 Ping pong 树状数组模板题

开始用瓜神说的方法撸了一发线段树,早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #includ