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. Android内存优化14 内存泄漏常见情况5 特殊对象造成的内存泄漏 WebView内存泄漏

    WebView造成内存泄露 关于WebView的内存泄露,因为WebView在加载网页后会长期占用内存而不能被释放,因此我们在Activity销毁后要调用它的destory()方法来销毁它以释放内存. ...

  2. Hibernate3的jar包

    一.hibernate3包说明 说明: Hibernate 软件包中的Hibernate3.jar 是我们需要使用的Hibernate 工具,其他引用的 Jar 文件位于lib 子目录下,Hibern ...

  3. ylbtech-LanguageSamples-Security(安全)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Security(安全) 1.A,Security 示例(Sample) 返回顶部 “安 ...

  4. 流畅的python第七章函数装饰器和闭包学习记录

    本章讨论的话题 python如何计算装饰器句法 python如何判断变量是不是局部的(通过函数内部是否给变量赋值过来判断是否是局部变量) 闭包存在的原因和工作原理(闭包是一种函数,它会保留定义函数时存 ...

  5. Perforce查看workspace sync到的changlist

    一 查看workspace sync到的changelist perforce的workspace其实是一些特定版本的文件的 结合,相比只将workspace对应到某个特定的changelist,此方 ...

  6. Spring框架学习(4)spring整合hibernate

    内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...

  7. 无废话MVC入门教程一[概述、环境安装、创建项目]

    (转载) 本文目标 1.对MVC有初步的了解 2.能够在VS2010的基础之上安装MVC3的开发和运行环境 3.对MVC框架有概括性的认识 本文目录 1.什么是MVC 2.VS2010安装MVC3 3 ...

  8. ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value:

    今天碰到一个相当棘手的问题,那就是ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value . 本来在本地测试是没有任 ...

  9. HTTP常用端口号与对应的服务说明

    常用端口号与对应的服务以及端口关闭 端口简介:本文介绍端口的概念,分类,以及如何关闭/开启一个端口 21端口:21端口主要用于FTP(File Transfer Protocol,文件传输协议)服务. ...

  10. websphere中的会话超时设置 和 web应用中web.xml中session-timeout关系

    Tomcat默认的会话的超时时间设置 设置Tomcat session有效期的三种方式有: 1.在tomcat/conf/web.xml中修改session-timeout的值,该设置是TOMCAT全 ...