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. 解决sqlite删除数据或者表后,文件大小不变的问题

    原因分析: sqlite采用的是变长纪录存储,当你从Sqlite删除数据后,未使用的磁盘空间被添加到一个内在的"空闲列表"中用于存储你下次插入的数据,用于提高效率,磁盘空间并没有丢 ...

  2. 手机网站和PC网站兼容的响应式网页设计

    今天跟大家介绍的这个网站叫 媒体查询  官网域名:http://mediaqueri.es/ 该酷站收集了很多响应式设计的案例.全部都是收集的一些励志精美而时尚的网站,使用媒体查询和响应的网页设计. ...

  3. mac securecrt无法记住密码的解决方法

    打开secureCRT,菜单preferences--general,找到mac options.然后去掉Use KeyChain选项,这样每次连接服务器后就会自动保存密码了.不同的版本可能这个选项的 ...

  4. 如何高效把一字节的位对换, bit0和bit7,bit1和bit6,以此类推.

    #include<stdio.h> #include<stdlib.h> //异或法 unsigned char byteReverse(unsigned char val) ...

  5. [开发工具]_[VS2010]_[vs2010的一个bug-使用stringstream时出现]

    1. 注冊Microsfot之后想提交bug, 发现有这个提示, 所以提交不了bug, 有能提交的提交下吧. You are not authorized to submit the feedback ...

  6. Java程序调用带参数的shell脚本返回值

    Java程序调用带参数的shell脚本返回值 首先来看看linux中shell变量(\(#,\)@,$0,$1,\(2)的含义解释 变量说明: -  \)$  Shell本身的PID(ProcessI ...

  7. 【Python】得到当前日期时间

    import time str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print(str)

  8. Postgresql监控pgwatch的搭建

    一,需要环境: You will need a handful of components to make this work: - Apache (webserver) #apache搭建web页面 ...

  9. Ubuntu下的多线程下载工具:MultiGet;并与 Firefox 建立关联 uget

    Ubuntu下非常给力的下载工具--uget+aria2 1.uget的安装: sudo add-apt-repository ppa:plushuang-tw/uget-stable sudo ap ...

  10. sse float 转int 截断和不截断

    之前, 我用sse指令, 想把float 型转成int, 不过其中遇到了一些困惑,就是截断和不截断的问题, 这个问题一直困扰我好集体, 最后终于解决了, 原来sse本身就有截断和不截断的指令. _mm ...