Ping pong

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

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
 

本题是2008年北京赛区的H题,大致题意为两个乒乓选手a和b想提升排名,但是通过比赛来提升自己的名次,于是他们找来一个同样也是乒乓选手的c作为裁判来抉择,

题目规定c必须在a和b之间,问对于所有的n个人,总共有多少中不同的可能....

思路:如果改变a和b,那么需要三个循环才能解决,显然会超时...

但是考虑c然后统计出小于c的有多少种可能x,大于c有多少种可能y..

很容易知道结果为x*y....

于是就可以推得公式: sum=Σi=0(xi*yi);

剩下的便是统计了...

这里我用的是BIT即树状数组

代码如下:

 //@coder Gxjun
//BIT algorithm
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 20000
#define val 100000
int bb[val+],aa[maxn];
//低位技术
int lowbit(int k)
{
return k&(-k);
// return k&(k&(k-1));
// return k&!(k-1);
}
void ope(int x)
{
while(x<=val)
{
bb[x]++;
x+=lowbit(x);
}
} int sum(int x)
{
int ans=;
while(x>)
{
ans+=bb[x];
x-=lowbit(x);
}
return ans;
}
int xx[maxn],yy[maxn];
int main()
{
int tt,nn,i;
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&nn);
for(i=;i<nn;i++)
scanf("%d",&aa[i]);
//求小于aa[i] 的个数在aa[0]~~aa[i-1]
memset(bb,,sizeof(bb));
for(i=;i<nn;i++)
{
xx[i]=sum(aa[i]-);
ope(aa[i]);
}
memset(bb,,sizeof(bb));
for(i=nn-; i>=;i--)
{
yy[i]=sum(aa[i]-);
ope(aa[i]);
}
__int64 res=;
for(i=;i<nn ;i++)
{
res+=(i-xx[i])*yy[i]+xx[i]*(nn-i--yy[i]);
}
printf("%I64d\n",res);
}
return ;
}

HDUOJ------2492Ping pong的更多相关文章

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

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

  2. POJ3928Ping pong[树状数组 仿逆序对]

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3109   Accepted: 1148 Descrip ...

  3. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  4. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  5. Mini projects #4 ---- Pong

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  6. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  7. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  8. POJ 3928 Ping pong(树状数组)

                                                                          Ping pong Time Limit: 1000MS   ...

  9. (转) Deep Reinforcement Learning: Pong from Pixels

    Andrej Karpathy blog About Hacker's guide to Neural Networks Deep Reinforcement Learning: Pong from ...

  10. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

随机推荐

  1. json-lib包笔记

    json-lib.jar开发包使用: 依赖包:commons-beanutils.jar;commons-httpclient.jar;commons-lang.jar;ezmorph.jar;不少人 ...

  2. android 利用 aapt 解析 apk 得到应用名称 包名 版本号 权限等信息

    在上传各大市场时发现 apk 上传后能自动解析出应用名称.包名.版本号.使用权限等信息,所以就研究了一下 1 直接解压 apk 解析  AndroidManifest.xml 是不行的,因为 apk ...

  3. Guava Files 源码分析(一)

    Files中的工厂 Files类中对InputStream, OutputStream以及Reader,Writer的操作封装了抽象工厂模式,抽象工厂是InputSupplier与OutputSupp ...

  4. C++代码文件名标准化处理工具

    工具功能:批量处理C++代码文件,将C++代码文件名中大写字母改为下划线+小写字母. 为了方便代码在不同平台下的移植,代码文件命名规范为:不使用大写字母,单词之间用下划线间隔开.为此写了这个小工具,将 ...

  5. android系统访问自己的tomcat服务器下的项目不能访问的原因

    今天做android的一个下载功能,用自己机子上的tomcat做服务器,在tomcat上下载东西,可是android系统老是提示错误说不能连接到我的tomcat,可是我明明启动了tomcat服务啊,而 ...

  6. 总结div里面水平垂直居中的实现方法

    最近经常碰到要垂直居中的问题,所以想着总结一下:关于如何设置小盒子在大盒子里面水平垂直方向同时居中的实现方法有很多种,下面仅列举了常用的几种. 首先看一下要实现的效果图及对应的html代码: < ...

  7. IOS开发之地图导航

    一.问题描述 现在很多的APP 都开始引入了地图和定位功能,包括一些餐饮业,团购等.他们都过定位和地图来让用户更加方便的根据自己的位置找到合适的目标,也就是说,现在地图定位已经不再是导航工具类,地图工 ...

  8. 【Python】Django删除数据迁移记录

    find . -path "*migrations*" -name "*.py" -not -path "*__init__*" -exec ...

  9. 编译和使用 MySQL C++ Connector

    记录编译 mysql C and C++ connector 和简单访问数据库. 环境: vs2012,  mysql 5.6.13, 基于x64 0. 软件包 mysql http://dev.my ...

  10. Cognos报表展示图片小技巧

    场景:在销售行业,比如手机,服装行业,如果仅仅的显示数字.文字那就显得不是很生动了,例如可以显示一下图片,那种样子的产品受大家喜欢. 样例1:在报表头都喜欢加上一些公司的logo,让报表看上去专业点. ...