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. appium+python自动化44-appium命令行模式

    前言 appium desktop有个客户端版本,每次运行脚本的时候都要去双击启动才能运行,很显然不太方便,影响效率.那么有没什么办法不启动桌面程序就能运行呢,比如cmd命令行执行? 环境: appi ...

  2. 关于DeploymentConfig的思考

    为什么是deploymentconfig而不是Kubernetes的deployment 在new-app的时候openshift直接创建了一个deploymentconfig并部署成rc,开始并不理 ...

  3. IDEA/Pycharm/Webstorm项目目录中的 Scratches and Consoles作用

    临时的文件编辑环境,通过临时的编辑环境,你可以写一些文本内容或者一些代码片段. 参考:https://segmentfault.com/a/1190000014202363 https://www.w ...

  4. iOS:quartz2D绘图(显示绘制在PDF上的图片)

    quart2D既可以用来绘制图像到pdf上,也可以从pdf上读取图像并显示出来.在使用这种方式之前,还有一种方式可以用来读取显示pdf上的图像,即使用UIWebView网页视图控件- (void)lo ...

  5. springmvc实现简单的拦截器

    SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的.在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方 ...

  6. VMware(bridge、NAT、host-only、custom)含义

    摘自: http://www.liangston.com/?post=48 VMware(bridge.NAT.host-only.custom)含义 作者:LiangSton 发布于:2012-1- ...

  7. 推断是否是有效的IP地址

    #include<stdio.h> #include<string.h> bool isValidIp(char *s) { int len=strlen(s); int i= ...

  8. PHP-时间小结

    //获得本周(本天)时间戳的起始和结束//本周星期一时间戳$monday = mktime(0, 0, 0, date("m",strtotime("last Monda ...

  9. PHP实现程序单例执行

    原创文章,转载请注明出处:http://huyanping.sinaapp.com/?p=222 作者:Jenner 一.场景描写叙述: 近期我们一块业务.须要不断的监听一个文件夹的变化.假设文件夹中 ...

  10. 1年内4次架构调整,谈Nice的服务端架构变迁之路

    Nice 本身是一款照片分享社区类型的应用,在分享照片和生活态度的同时可以在照片上贴上如品牌.地点.兴趣等tag. Nice从2013.10月份上线App Store到目前每天2亿PV,服务端架构经过 ...