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. js中几种常见的方法的实例 shift,unshift,push,prop

    1.shift()定义和用法 shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值. 语法:arrayObject.shift() 返回值:数组原来的第一个元素的值. 说明:如果 ...

  2. linux下编译运行C程序

    GCC是Linux操作系统下一个非常重要的源代码编译工具,有着许多重要的选项,支持许多不同语言的编译,如C.C++.Ada.Fortran.Objective.Perl.Python.Ruby以及Ja ...

  3. Delphi控件开发浅入深出(三)

    三.开关控件TlincoSwitch 用过Delphi1(好古老的东东呀!)的人相信都记得这个开关控件 ,不知道当初Borland为什么把这么一个在开发普通应用程序中应用不到的工控控件放到Delphi ...

  4. Ubuntu - 硬盘分区、格式化、自动挂载配置

    Ubuntu系统的硬盘空间不够用了,需要增加新的硬盘扩容.将硬盘分区.格式化.自动挂载配置的整个过程记下来,备忘. 运行环境 | Enviroment Ubuntu 10.10 一.硬盘分区 | Ha ...

  5. 大气散射 Aerial Perspective

    http://mathinfo.univ-reims.fr/IMG/pdf/PreethamSig2003CourseNotes.pdf https://blog.csdn.net/toughbro/ ...

  6. Android视图SurfaceView的实现原理分析(示例,出错代码)

    在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...

  7. CentOS6.8 4.4.43内核 安装PF_RING

    环境: 系统:CentOS 6.8 内核版本:4.4.43 PF_RING版本:6.9.0 编译PF_RING需要内核源码,由于我的机器上只有4.4.43版本的modules和4.4.43的源码,并没 ...

  8. php 获取/设置用户訪问页面语言类

    User Language Class 获取/设置用户訪问的页面语言,假设用户没有设置訪问语言.则读取Accept-Language. 依据用户选择的语言显示相应的页面(英文.中文简体,繁体中文) U ...

  9. HDU 4554 叛逆的小明

    叛逆的小明 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submiss ...

  10. Oracle DMP 操作笔记之根据DMP逆向推导出导出的表空间名称

    最近在带着一群.NET新兵们在开发和升级一套系统,本人虽然工作好几年,但是也是属于啥都懂一点,啥都不会的队伍,碰到新兵更是蛋都碎了,还特别拘谨,为啥新兵们都是基础知识很不错的,看来要好好练习内功了,好 ...