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. Mysql表锁定解决

    #查看进程SELECT *FROM information_schema.processlistWHERE USER = 'root' AND state LIKE 'Waiting%';#杀掉进程K ...

  2. JS数组操作常用方法

    toString():把数组转换成一个字符串 toLocaleString():把数组转换成一个字符串 join():把数组转换成一个用符号连接的字符串 shift():将数组头部的一个元素移出 un ...

  3. Thinkphp的页面判断

    目前在做的系统中,涉及到了前端页面的判断,比如性别在数据库中存储为0或1,而页面显示应该为“男”或“女”,这里就需要用到页面判断. 比如, <volist name="st" ...

  4. 黑马程序员-- C语言交换两个整数变量值几种函数比较

    总结了C语言中几种交换两个整数数值的函数,欢迎交流 #include <stdio.h> 使用多种交换变量值的函数比较 方法一:使用第三方临时变量 这种函数a,b只是值传递,实质上不能修交 ...

  5. QT中进度条的使用

    在QT中可以用QProgressBar或着QProgressDialog来实现进度条. QProgressBar的使用 首先在designer中拖一个按钮和进度条部件,按下面初始化 //补充:下面两句 ...

  6. Linux 内核无线子系统

    Linux 内核无线子系统 浅谈 Linux 内核无线子系统 Table of Contents 1. 全局概览 2. 模块间接口 3. 数据路径与管理路径 4. 数据包是如何被发送? 5. 谈谈管理 ...

  7. vmware 使用技巧

    1.虚拟机如何进入BIOS界面? 方法: 1) 开机按F2 2) 若来不及按F2的话,可以通过以下 2. 如何让虚拟机进入安全模式? 方法:开机快速按F8 3. 当不能连接到vCenter Serve ...

  8. linux----命令替换

    0.命令替换.它的大概意思是.命令在脚本中只起一个站位符的作用:在命令运行时它会被命令自己的执行结果所替换. 1.使用格式: 1.$(command) 2.`command` 2.使用举例: 1.:打 ...

  9. delphi 关于命名

    请告别 TMyXXX 的命名方法吧... 程序名: Demo.exe 窗体:TFrmDemo ,窗体文件 uFrmDemo.Pas DataModule: TDMDemo, 窗体文件 uDMDemo. ...

  10. Android之CookieStore的持久化

    CookieStore是一个对象,有的服务端 ,比如.net,保持登录状态不是用httpclient.addHeader(“cookie”,SessionId),而是用httppost.setCook ...