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 < ...
随机推荐
- 【Network】修改docker启动默认网桥docker0为自定义网桥
自定义网桥 除了默认的 docker0 网桥,用户也可以指定网桥来连接各个容器. 在启动 Docker 服务的时候,使用 -b BRIDGE或--bridge=BRIDGE 来指定使用的网桥. 如果服 ...
- Mysql学习笔记(附一)
关于外键约束关系下修改或者删除表的方法: http://wenku.baidu.com/link?url=RRaI160kvsdf7ibMLqxN815RvStSyenz_-ig1ONfpRfpfFp ...
- appium java 环境搭建
appium是一款open source 移动自动化测试框架,既支持Android 也支持IOS. appium 基于webdriver协议执行测试脚本. 今天,如炒剩饭,介绍一下搭建appium A ...
- Feature Access
在ArcGIS Server中发布支持Feature Access地图服务,你需要知道的几点: 所绘制的mxd地图文件中包含的数据,必须来自企业级数据库链接: mxd中包含的所有图层的数据,必须来自同 ...
- maven学习(中)- 私服nexus搭建
接上回继续,相信大家对maven每次都联网从国外站点下载依赖项的龟速网络已经不坎忍受了,今天先来看看如何搭建"仓库私服",目前nexus是使用比较广泛的私服软件之一,下面将介绍基本 ...
- javascript 核心语言笔记 4 - 表达式和运算符
表达式(expression)是 JavaScript 中的一个短语(phrases),JavaScript 解释器会将其计算(evaluate)出一个结果.程序中的常量.变量名.数组访问等都是表达式 ...
- Java实现画八卦
八卦是由多个圆叠加而成,如果我们让每个圆都有自己的颜色,那么具体结构便一目了然,如下图所示: 显然只要令对应的圆颜色相同,就能达到我们预期的效果. 用Java就能轻松画出来: import java. ...
- android include进来的组件 调用其子元素
include标签包裹着一个可复用的布局: <include layout="@layout/footer_detail" android:id="@+id/foo ...
- hosts文件权限导致监听无法启动
1.所属系统:2.问题描述:3.解决4.参考???疑点:为什么修改主机名会使hosts文件权限发生改变 1.所属系统: ZHJS2#[/]uname -a HP-UX ZHJS2 B.11.31 U ...
- 【DWR系列04】- DWR配置详解
table { margin-left: 30px; width: 90%; border: 1px; border-collapse: collapse } img { border: 1px so ...