Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4874    Accepted Submission(s): 1777

Problem 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
题解:

把运动员排成一排,对于其中任意一个位置i上的人来说,如果他作为裁判,则有这么两种可能:

1.左边的某人能力值低于他,右边高于他;

2.左边的某人能力值高于他,右边低于他。

记左边比他小的人数为l[i],右边比他小的人数为r[i],

那么左边比他大的人数为i-1-l[i],右边比他大的人数为n-i-r[i],

则i作为裁判就有l[i]*(n-i-r[i])+(i-1-l[i])*r[i];

前缀 后缀比i人大的数用树状数组求;

那么代码为:

要用long long ;

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<algorithm>
//#define LOCAL
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
int tree[MAXN+],a[MAXN],bl[MAXN],br[MAXN];
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXN){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int T,N;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=;i<=N;i++)scanf("%d",a+i);
memset(tree,,sizeof(tree));
for(int i=;i<=N;i++){
bl[i]=SUM(a[i]);
update(a[i]);
}
memset(tree,,sizeof(tree));
for(int i=N;i>;i--){
br[i]=SUM(a[i]);
update(a[i]);
}
long long ans=;
for(int i=;i<=N;i++){
ans+=bl[i]*(N-i-br[i])+br[i]*(i--bl[i]);
}
printf("%lld\n",ans);
}
return ;
}

Ping pong(树状数组经典)的更多相关文章

  1. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  3. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  4. LA4329 Ping pong 树状数组

    题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...

  5. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  6. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  7. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  8. LA 4329 - Ping pong 树状数组(Fenwick树)

    先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...

  9. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

随机推荐

  1. 001.web前端-学习了解

    学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1.html(Hypertext Markup Language)—— ...

  2. WEB开发之如何改善PHP开发方式

    改善PHP开发方式一般可以分为以下几种实现方式: 1.组织和样式     找出一种适合你的组织方法和编码样式,并且一直坚持下去,这样的话,你的代码的组织和布局会变得十分有条理.我们不应该轻视代码的组织 ...

  3. php部分学习笔记

    [web 开发分为]1. 静态web 开发(html 页面) 如果我们的一个页面,始终是一成不变的,则就是属于静态web 开发,一般讲用html 技术就ok2. 动态web 开发 比如: 我们需要发帖 ...

  4. zabbix 的安装

    第一步:官方的源: rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.r ...

  5. ArcEngine 9.3 学习笔记(六):图层符号化(COlorRamp,MarkerSymbol,LineSymbol,FillSymbol,TextSymbol,3DChartSymbol,使用ServerStyle符号库,FeatureRender,RasterRender)

    第四章 图层符号化 AE9.3 提供了SymbologyControl控件,用于显示ARCGIS符号库中的符号. 组件库中的组件对象分为Color(颜色),Symbol(符号),Render(渲染)三 ...

  6. Jimmy Choo_百度百科

    Jimmy Choo_百度百科 Jimmy Choo

  7. 面向对象程序设计-C++_课时24多态的实现

    所有带virtual的类的对象,里面最上面有一个隐藏的指针vptr,指向一张表vtable #include <iostream> using namespace std; class A ...

  8. mysql字符集编码乱码测试如下

    创建三个表tb_latin1,tb_utf8,tb_gbk,编码分别为latin1/utf8/gbk “你好a”字符串编码如下GBK : %C4%E3 %BA%C3 %61UTF-8 : %E4%BD ...

  9. 列表的实现-----数据结构与算法JavaScript描述 第三章

    实现一个列表 script var booklist = new List(); booklist.append('jsbook'); booklist.append('cssbook'); book ...

  10. WinForm中快捷键与组合按键的设置方法

    每个窗体都有这样3个事件:KeyDown.KeyPress.KeyUp,KeyDown和KeyPress都是按键按下事件,但KeyDown用的是KeyCode跟键盘各个按键相对应,它对应Keys枚举, ...