HDU 2492 Ping pong (数状数组)
Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2611 Accepted Submission(s): 973
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?
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
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int N=; int n,arr[N+],a[N+];
int lmin[N+],lmax[N+],rmin[N+],rmax[N+]; int lowbit(int x){
return x&(-x);
} void update(int i,int val){
while(i<=N){
arr[i]+=val;
i+=lowbit(i);
}
} int Sum(int i){
int ans=;
while(i>){
ans+=arr[i];
i-=lowbit(i);
}
return ans;
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(arr,,sizeof(arr));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++){
update(a[i],);
lmin[i]=Sum(a[i]-); //求左边有多少个数比自己小
lmax[i]=Sum(N)-Sum(a[i]); //求左边有多少个数比自己大
}
memset(arr,,sizeof(arr));
for(int i=n;i>=;i--){
update(a[i],);
rmin[i]=Sum(a[i]-); //求右边有多少个数比自己小
rmax[i]=Sum(N)-Sum(a[i]); //求右边有多少个数比自己大
}
long long ans=;
for(int i=;i<=n;i++)
ans+=lmin[i]*rmax[i]+lmax[i]*rmin[i]; //1.左边比自己大的数*右边比自己小的数
//2.左边比自己小的数*右边比自己大的数 ,相加就是总和
cout<<ans<<endl;
}
return ;
}
HDU 2492 Ping pong (数状数组)的更多相关文章
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)
Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)
主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Descript ...
- UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
随机推荐
- Managed C++ dll: #define _AFXDLL or do not use /MD[ d]?
[问题] Hello all I'm writing a managed C++ dll with will be acting as an intermediate between a lo ...
- js文件流下载通用方法
通常我们会用到文件流下载文件,下面给大家一个通用的文件流下载的js /* *下载文件 * options:{ * url:'', //下载地址 * isNewWinOpen:false,是否新窗口打开 ...
- GPUImage API 文档之GPUImageOutput类
GPUImageOutput类将静态图像纹理上传到OpenGL ES中,然后使用这些纹理去处理进程链中的下一个对象.它的子类可以获得滤镜处理后的图片功能.[本文讲的很少,由于有许多地方不清楚,以后会更 ...
- 如何让windows更高效?
首先解释一下个标题: "让windows更高效,既指让windows更友好更优化,也指可以让使用windows来工作或学习的人更高效的工作学习." 解释下本文的动机: 指导我自己或 ...
- Mybatis特殊值Enum类型转换器-ValuedEnumTypeHandler
引言 typeHandlers 阅读官方文档 typeHandlers 一节 {:target="_blank"} MyBatis 在预处理语句(PreparedStatement ...
- postman添加权限验证
Basic Auth 输入用户名和密码,点击 Update Request 生成 authorization header 一种身份验证 分类: postman学习笔记
- k-means算法MATLAB和opencv代码
上一篇博客写了k-means聚类算法和改进的k-means算法.这篇博客就贴出相应的MATLAB和C++代码. 下面是MATLAB代码,实现用k-means进行切割: %%%%%%%%%%%%%%%% ...
- J2EE开发时的包命名规则,养成良好的开发习惯
代码编写规范目的:能够在编码过程中实现规范化,为以后的程序开发中养成良好的行为习惯.代码编写规范使用范围:J2EE项目开发.包命名规范:目的:包的命名规范应当体现出项目资源良好的划分 servlet类 ...
- Java之所有对象的公用方法>9.Always override hashCode when you override equals
You must override hashCode in every class that overrides equals.
- exception java.lang.OutOfMemoryError: Java heap space
1.情景展示 java内存溢出异常,将程序代码问题排除在外,如何增大JVM的使用内存? 2.解决方案 在eclipse中的解决办法:增大你要运行的测试类的内存分配. 点击运行或debug按钮旁的 ...