HDUOJ------2492Ping pong
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即树状数组
代码如下:
1 //@coder Gxjun
2 //BIT algorithm
3 #include<stdio.h>
4 #include<string.h>
5 #include<stdlib.h>
6 #define maxn 20000
7 #define val 100000
8 int bb[val+5],aa[maxn];
9 //低位技术
10 int lowbit(int k)
11 {
12 return k&(-k);
13 // return k&(k&(k-1));
14 // return k&!(k-1);
15 }
16 void ope(int x)
17 {
18 while(x<=val)
19 {
20 bb[x]++;
21 x+=lowbit(x);
22 }
23 }
24
25 int sum(int x)
26 {
27 int ans=0;
28 while(x>0)
29 {
30 ans+=bb[x];
31 x-=lowbit(x);
32 }
33 return ans;
34 }
35 int xx[maxn],yy[maxn];
36 int main()
37 {
38 int tt,nn,i;
39 scanf("%d",&tt);
40 while(tt--)
41 {
42 scanf("%d",&nn);
43 for(i=0;i<nn;i++)
44 scanf("%d",&aa[i]);
45 //求小于aa[i] 的个数在aa[0]~~aa[i-1]
46 memset(bb,0,sizeof(bb));
47 for(i=0;i<nn;i++)
48 {
49 xx[i]=sum(aa[i]-1);
50 ope(aa[i]);
51 }
52 memset(bb,0,sizeof(bb));
53 for(i=nn-1; i>=0;i--)
54 {
55 yy[i]=sum(aa[i]-1);
56 ope(aa[i]);
57 }
58 __int64 res=0;
59 for(i=0;i<nn ;i++)
60 {
61 res+=(i-xx[i])*yy[i]+xx[i]*(nn-i-1-yy[i]);
62 }
63 printf("%I64dn",res);
64 }
65 return 0;
66 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Qt官方示例-样式表
- 入坑 LinkedList,i 了 i 了
- Elasticsearch 利用API进行搜索
- 通过 Serverless Regsitry 快速开发与部署一个 WordCount 实例
- 聊聊dubbo-go的gracefulShutdownFilter
- 强烈推荐:2020年15道优秀的TypeScript练习题 (上集)
- 聊聊dubbo-go的GenericFilter
- Salesforce LWC学习(十九) 针对 lightning-input-field的label值重写
- MySQL定时备份方案
- Nginx多方面调优策略
- 太厉害了!这应该是目前Redis可视化工具最全的横向评测
- pip install时timeout设置
- 聊聊dubbo-go的TpsLimitFilter
- 聊聊dubbo-go的TokenFilter
- 你这磨人的小妖精——选中文本并标注的实现过程