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. scikit-learn(window,linux)安装

    scikit-learn是python的机器学习库 记录下载window中和linux中如何下载scikit-learn 方法一 直接下载Anaconda 这是一个非常齐全的python发行版本,里面 ...

  2. java缓存适合使用的情况

    并非所有的情况都适合于使用二级缓存,需要根据具体情况来决定.同时可以针对某一个持久化对象配置其具体的缓存策略. 适合于使用二级缓存的情况: 1.数据不会被第三方修改 一般情况下,会被hibernate ...

  3. Maven nexus 安装nexus私服出现的两个问题

    1. 在win10中安装nexus时提示:wrapper | OpenSCManager failed - 拒绝访问. (0x5) 主要是没有权限.需要以管理员的身份运行 如果你是直接点击 start ...

  4. 云计算之路-Azure vs 阿里云:从负载均衡中摘/挂虚拟机

    @小尾鱼 在 试用Azure:上不了高速的跑车,无法跨Cloud Service的DNS服务器一文的评论中提了一个很好的问题: 问个问题,使用了负载均衡以后,程序发布的时候博客园是怎么避免用户访问到正 ...

  5. android不同机型上界面适配问题

    android中长度有:dp(或者dip device independent pixels)一种基于屏幕密度的抽象单位.在每英寸160点的显示器上.1dp=1px. 不同设备有不同的显示效果.这个和 ...

  6. LR打不开浏览器的解决方法

        很久没用LoadRunner了,今天想复习一下,免得技能生疏,安装了一个LR11,跑一下,竟然打不开IE浏览器: 这时肯定是靠谷哥跟度娘的,经过一轮搜索,可以解决打开IE了,但录制不了解决,又 ...

  7. ionic 项目使用百度地图插件(cordova-qdc-baidu-location)

    现在我们使用'Weizhe He'提供的cordova-qdc-baidu-location来尝试创建简单的定位app. Stpe1:创建一个项目 Stpe2:申请百度地图API秘钥     应用类型 ...

  8. ionic 图片加载失败,显示默认图片代替

    1.首先编写自定义指令 angular.module('starter.directives', []) //当图片找不到事显示替代图片 .directive("errSrc", ...

  9. leetcode第一刷_Combination Sum Combination Sum II

    啊啊啊啊.好怀念这样的用递归保存路径然后打印出来的题目啊.好久没遇到了. 分了两种,一种是能够反复使用数组中数字的,一种是每一个数字仅仅能用一次的.事实上没有多大差别,第一种每次进入递归的时候都要从头 ...

  10. js 判断是否是IE浏览器及ie版本

      方式一:只判断是否是ie浏览器 /** * 判断是否是IE浏览器,支持IE6-IE11 */ function isIE() { //ie? if (!!window.ActiveXObject ...