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 < ...
随机推荐
- - > code vs 3038 3n+1问题(递归)
3038 3n+1问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 3n+1问题是一个简单有趣而又没有解决的数 ...
- myabatis oracle 调用存储过程返回list结果集
Mapper.xml 配置 <resultMap type="emp" id="empMap"> <id property="emp ...
- Ubuntu实用小指令
1.Ubuntu命令行下打开图形界面的文件夹 可以使用 nautilus path 为使用方便,可以给命令nautilus加一个别名cd /home/usernamevi .bash_aliases在 ...
- linux ps命令,查看进程cpu和内存占用率排序(转)
使用以下命令查看: ps -aux | sort -k4,4n ps auxw --sort=rss ps auxw --sort=%cpu linux 下的ps命令 %CPU 进程的cpu占用率 % ...
- IO边读边写
using (FileStream fs = new FileStream(@"C:\Users\Desktop\lijia1.txt",FileMode.Open)) ...
- JAVASCRIPT常用API总结
目录 元素查找 class操作 节点操作 属性操作 内容操作 css操作 位置大小 事件 DOM加载完毕 绑定上下文 去除空格 Ajax JSON处理 节点遍历 元素查找 // Node docume ...
- 重装系统后如何删除系统自带的office2003
背景:操作系统重装后,系统自带的office2003版本太低不好使用,需要安装更高版本的如2010,但是卸载时提示“无法打开此修补程序包……”,从而无法卸载.经网上查询,采用以下解决办法: 在注册表中 ...
- 【转】linux sort 命令详解
sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按AS ...
- 改变linux默认配色方案(dircolors和dircolors-solarized使用)
前言 前几天刚买了阿里云的云服务器,今天使用putty进入服务器,发现linux默认的bash配色实在太丑. 特别是文件夹显示为深蓝色,到了白天,和黑色背景一搭配,根本看不清文字. 好在在github ...
- 3.4.4 数据预留和对齐(skb_reserve, skb_push, skb_put, skb_pull)
转自:http://book.51cto.com/art/201206/345043.htm <Linux内核源码剖析:TCP/IP实现>本书详细论述了Linux内核2.6.20版本中TC ...