Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2611    Accepted Submission(s): 973

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
 
Source
 
Recommend
gaojie
 
  题目大意:每一个人都有一个实力值,顺序就是他的位置,要求寻找一个对手,然后再寻找一个裁判,要求裁判的实力再他们之间,而且位置要在他们两人的中间,这样可以组成一场比赛。问总共可以组织多少场比赛?
      解题思路:以自己为裁判,然后找左边有多少人比较小,再找右边有多少人比自己大,然后两个数相乘就是以他为裁判的比赛场数,当然还有相反的,找右边比自己小的,左边比自己大的,再相乘相加。这样子就变成了:和找逆序数、顺序数差不多了,找左边(右边)比自己大(小)有多少个数,用树状数组最好最快!所以两个for循环就全部找到,然后相乘相加,
 
#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 (数状数组)的更多相关文章

  1. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  2. 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 ...

  3. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. POJ 3928 &amp; HDU 2492 Ping pong(树阵评价倒数)

    主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Descript ...

  7. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  8. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  9. LA4329 Ping pong 树状数组

    题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...

随机推荐

  1. Linux:磁盘挂载

    本来虚拟centos的服务器的磁盘分配的就不大,之前只分配了20G的样子,由于最近有装了不少软件,比如nifi压缩版就有1.2G的大小,一下子没有磁盘资源了.今晚就折腾在这事上了. [root@mas ...

  2. Spark:求出分组内的TopN

    制作测试数据源: c1 85 c2 77 c3 88 c1 22 c1 66 c3 95 c3 54 c2 91 c2 66 c1 54 c1 65 c2 41 c4 65 spark scala实现 ...

  3. NSProxy

    NSProxy类在分布式对象架构中是很重要的.由于作用比较特别,NSProxy在Cocoa程序中出现频率很低. NSProxy 是一个抽象类,它为一些表现的像是其它对象替身或者并不存在的对象定义一套A ...

  4. JavaScript 时间、格式、转换及Date对象总结

    悲剧的遇到问题,从前台得到时间,“Tue Jan 29 16:13:11 UTC+0800 2008”这种格式的,想再后台解析成想要的格式,但是在后台就是解析不了SimpleDateFormat也试着 ...

  5. Linux开机自动启动某一程序

    Linux开机启动程序详解我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. ...

  6. Mongodb3安装授权

    (1) mongodb 官网下载解压包mongodb-win32-x86_64-3.0.7.zip解压释放在d盘,目录为mongodb,接下来手动创建data文件夹和log文件夹分别用于存放数据和日志 ...

  7. SHELL字符串使用总结

    1.获取字符串的长度,${#str} #设置字符串 $ str="liqiu" #打印字符串 $ echo $str liqiu #继续打印字符串 $ echo ${str} li ...

  8. UBUNTU 字符界面来回切换

    图形界面切换到字符界面: 实体机:Ctrl + Alt + F1 VMware虚拟机:按下ALT+CTRL+SPACE(空格),ALT+CTRL不松开,再按F1.这样就可以切换到字符界面 字符界面切换 ...

  9. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  10. [转]Spring MVC之@RequestMapping 详解

    前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, ...