Ping pong(树状数组经典)
Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4874 Accepted Submission(s): 1777
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?
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).
3 1 2 3
把运动员排成一排,对于其中任意一个位置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(树状数组经典)的更多相关文章
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
随机推荐
- 关于ie6中使用css滤镜[_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/*.png',sizingMethod='scale')]后链接无法点击的问题
RT,我做的一个效果是试用png图做背景,通过_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/*.png' ...
- SQLSERVER 使用WITH函数查找时间点最大数据行
--表结构及数据: DTIME TYPE MONEY 2015-10-14 13:50:35.000 shopping 20 2015-10-21 13:51:24.000 shopping 40 ...
- Xampp Linux应用
一.基本操作: 1.Xampp安装包下载: https://www.apachefriends.org/index.html 2.安装与配置: 将xampp-linux-x64-5.6.3 ...
- CentOS下重新安装yum
1,下载最新的yum-3.2.28.tar.gz并解压 #wget http://yum.baseurl.org/download/3.2/yum-3.2.28.tar.gz#tar xvf yum- ...
- android:configChanges 屏幕横竖屏切换
出处:http://blog.csdn.net/djy1992/article/details/9378195 ---> android:screenOrientation="por ...
- linux去掉svn里记住的用户名密码
去掉svn里记住的用户名密码 删除掉 c:\Documents and Settings\administrator\ApplicationData\Subversion\auth\svn.sim ...
- 反恐训练营(LCS)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- android高仿微信拍照、多选、预览、删除(去除相片)相冊功能
先声明授人与鱼不如授人与渔,仅仅能提供一个思路,当然须要源代码的同学能够私下有偿问我要源代码:QQ:508181017 工作了将近三年时间了,一直没正儿八经的研究系统自带的相冊和拍照,这回来个高仿微信 ...
- 使用 Eclipse Memory Analyzer 进行堆转储文件分析
Eclipse Memory Analyzer(MAT)是著名的跨平台集成开发环境 Eclipse Galileo 版本的 33 个组成项目中之一,它是一个功能丰富的 JAVA 堆转储文件分析工具,可 ...
- DataTable去除重复行
//抽取dt中的特定列"Region","Cur","Year"组合成新的dat DataTable dat = dt.DefaultVie ...