HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492
Ping pong
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
话不多说 直接上代码
解释都在代码中标注了
#include<stdio.h>
#include<string.h>
#define size 100100
int n,c[size],x1[size],x2[size],y1[size],y2[size],a[size]; int Lowbit(int k)
{
return (k&-k);
}
void update(int pos,int num)
{
while(pos<size)//重要 是size 而不是<=n
{
c[pos]+=num;
pos+=Lowbit(pos);
}
}
int sum(int pos)
{
int s=;
while(pos>)
{
s+=c[pos];
pos-=Lowbit(pos);
}
return s;
} int main()
{
int i,cas;
scanf("%d",&cas);
while(cas--)
{
memset(c,,sizeof(c));
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
int k1;
k1=sum(a[i]);
x1[i]=k1; //输入的i个数中 有k1个比a[i]小
x2[i]=i--k1; //输入的i个数中 有k1个比a[i]大
update(a[i],);
}
memset(c,,sizeof(c));
int j=;//代表现在输入的数的个数
for(i=n;i>=;i--,j++)
{
int k1;
k1=sum(a[i]);
y1[i]=k1;//输入a[i]后输入的那些数中有多少个比a[i]小的
y2[i]=j--k1; //输入a[i]后输入的那些数中有多少个比a[i]大的
update(a[i],);
}
__int64 ans=;
for(i=;i<=n;i++)
{
// printf("x1[%d]=%d x2[%d]=%d y1[%d]=%d y2[%d]=%d\n",i,x1[i],i,x2[i],i,y1[i],i,y2[i]);
ans+=x1[i]*y2[i]+x2[i]*y1[i];
// ans+=x1[i]*x2[i];
}
printf("%I64d\n",ans);
} }
HDU 2492 Ping pong (树状数组)的更多相关文章
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 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> ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
随机推荐
- EndNote(二)之英文引文导入方式
在上一篇EndNote教程(一)--基本介绍中介绍了基本使用,今天将来介绍如何将常用文献引文导入EndNote中. EndNote在文献管理方面有着很多优点,可以节约很多精力.但是,不同文献查询网站可 ...
- trap命令
一. [root@OEL6-38 arun]# vim yy.sh --------------------- 参考: http://blog.csdn.net/holandstone/article ...
- connect mysql from another host
vim /etc/mysql/my.cnf bind-address = 0.0.0.0 /etc/init.d/mysql restart mysql -uroot -p1234 # grant a ...
- Jade 模板引擎使用
在 Express 中调用 jade 模板引擎 jade 变量调用 if 判断 循环 Case 选择 在模板中调用其他语言 可重用的 jade 块 (Mixins) 模板包含 (Includes) 模 ...
- Java:方法的参数是传值还是传引用
Java中方法的参数总是采用传值的方式. 下列方法欲实现对象的交换,但实际上是不能实现的. public void swap(simpleClass a,simpleClass b){ simpleC ...
- C语言移位算符">>"
右移算符>>是将二进制数的每一位右移.如:a=32,a>>2,就是将32的二进制数100000每一个二进制位向右移动两位.得到的是二进制数1000,也就是8. #include ...
- java中cookie存取值
cookie存值: Cookie userCookie=new Cookie("loginInfo",loginInfo); userCookie.setMaxAge(30*24* ...
- mysql高并发和表类型
高并发:http://www.cnblogs.com/wangchaozhi/p/5061378.html 表类型:http://www.xiaoxiaozi.com/2009/07/14/1171/
- $stateProvider
在你的应用中大多数状态都有与其相关联的 url,路由控制不是设计完成 state 之后的事后想法,而是开始开发时就应该考虑的问题. 这里是如何设置一个基本url. 12345 $stateProvid ...
- MySql触发器语法总结
经过昨天多次失败,今天终于将我要实现的触发器功能写成功了,触发器代码如下: -- use dbfortest; drop trigger if exists tg_before_insert_on_d ...