Ping pong
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2302   Accepted: 879

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个乒乓球爱好者,经常组织比赛,每个人都有一个不同的技能值ai,每场比赛需要三个人,一个裁判,两个队员,有个奇怪的规定,裁判必须住在两名选手中间,并且技能也在两者之间,
求以功能组织多少场比赛;
解:考虑每一个人当裁判的时候,前面大于他的,后面小于他的,前面小于他的,后面大于他的,相乘并相加,然后统计:
AC代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = ;
const int M = ;
int x[M],y[M],ymin[M],ymax[M];
int a[N],lef[N],right[N],leftm[N];
int n;
int lowbit(int x)
{
return x&(-x);
}
void init( )//统计整体中小于等于i的数共有多少个,表示为ymin[i]
{
for(int i =; i<=M; i++)
{
ymin[i] = ymin[i-]+y[i];//n个数中共有多少个小于等于i,以后要减去1;
ymax[i] = n - ymin[i];//n个数中有多少大于i的;
}
}
void add(int i,int c)//插入一个数,计算一下后面的
{
while(i<=M)//第一次提交写了个N,于是wa啦
{
x[i] = x[i]+c;
i=i+lowbit(i);
}
}
int solve(int c)
{
int sum = ;
while(c>)
{
sum = sum+x[c];
c = c-lowbit(c);
}
return sum;
}
int main()
{
int T;
long long ans;
scanf("%d",&T);
while(T--)
{
ans = ;
memset(x,,sizeof(x));
memset(y,,sizeof(y));
scanf("%d",&n);
for(int i =; i<=n; i++)
{
scanf("%d",&a[i]);
y[a[i]] = ;
}
init( );
for(int i = ;i<=n;i++)
{
add(a[i],);
lef[i] = solve(a[i]-);//求前面小于a[i]的数;
leftm[i] = i--lef[i];//求前面大于a[i] 的数;
int ma = ymax[a[i]] - leftm[i];//后面大于a[i]的数;
int mb = ymin[a[i]] - - lef[i];//后面小于a[i]的数;
ans = ans + lef[i]*ma +leftm[i]*mb;//前大后小,前小后大;
}
printf("%lld\n",ans);
}
return ;
}

poj Ping pong LA 4329 (树状数组统计数目)的更多相关文章

  1. LA 4329 (树状数组) Ping pong

    第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...

  2. LA 4329(树状数组)

    题目描述: N <tex2html_verbatim_mark>(3N20000) <tex2html_verbatim_mark>ping pong players live ...

  3. HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

    Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...

  4. 算法竞赛入门经典 LA 4329(树状数组)

    题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...

  5. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  6. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  7. poj 3321 Apple Tree(一维树状数组)

    题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...

  8. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  9. POJ 3378 Crazy Thairs(树状数组+DP)

    [题目链接] http://poj.org/problem?id=3378 [题目大意] 给出一个序列,求序列中长度等于5的LIS数量. [题解] 我们发现对于每个数长度为k的LIS有dp[k][i] ...

随机推荐

  1. Unity3d设计模式之单例模式

    单例模式我相信是所有设计模式之中运用最广泛的设计模式之一. 今天我们就来看看在unity中如何使用单例模式,在unity中,我们分两种单例,一种是继承monobehavior的单例,一种是普通单例. ...

  2. [转]SSIS ADO.NET vs OLEDB

    本文转自:http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1a9e3670-9685-4943-913b-123ecf248a9c/ol ...

  3. [转]sqlserver2008锁表语句详解

    本文转自:http://xue.uplook.cn/database/sqlserver/801760.html 锁定数据库的一个表 代码如下: SELECT * FROM table WITH (H ...

  4. PowerShell中的一个switch的例子

    在这个例子中, 应该注意 Switch语句里对数字范围条件的使用 break的使用 字符串的拼接 数组的声明   ) foreach ($element in $array) { switch($el ...

  5. JVM组成部分以及内存模型

    一.JVM的组成部分 我们先把JVM这个虚拟机实现机制画出来,例如以下图所看到的: 从这个图中能够看到,JVM是执行在操作系统之上的,它与硬件没有直接的交互. 我们再来看下JVM有哪些组 成部分,例如 ...

  6. Jconsole

    Jconsole 1.1 简介以及连接 JConsole是一个基于JMX的GUI工具,用于连接正在运行的JVM,它是Java自带的简单性能监控工具.下面以对tomcat的监控为例,带领大家熟悉Jcon ...

  7. JMS与Spring之二(用message listener container异步收发消息)

    转自:http://blog.csdn.net/moonsheep_liu/article/details/6684948

  8. Navicat for SQL Server创建连接提示错误08001怎么办

    创建连接之后提示如下错误 打开SQL Server配置工具,把能打开的都打开(什么远程连接,什么SQL Server Browser之类的) 你再创建连接的时候就有不止一个连接了,连那些Named P ...

  9. Hibernate关系映射(一) 基于外键的单向一对一

    模拟用户和地址的映射关系,一个用户只有一个地址,用户知道地址,但是地址不知道用户.用户对地址的单向一对一映射. 一.建立实体类 Account.cs类 package com.lxit.entity; ...

  10. BroadcastReceiver应用详解——广播

    转自:http://blog.csdn.net/liuhe688/article/details/6955668 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收 ...