首页 > 代码库 > HDU2492 Ping pong
HDU2492 Ping pong
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492
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?
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).
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 Asia Regional Beijing
题意:
有n个人要进行乒乓球比赛每一个人都一个技术值,每个人出现的次序就是
他们住的位置,现在要求进行一场比赛,三个人,裁判的技术值在两个人的
中间,位置也在两个人的中间,问一共可以进行这种比赛多少次。
分析:
采用树状数组,先从左到右计算左边大的个数,左边小的个数,再从右到
做计算右边大和小的个数,然后交叉相乘取和就可以了
代码如下:#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int MAX = 100005; int a[MAX] , cnt[MAX] , leftMax[MAX] ; int leftMin[MAX] ,rightMax[MAX] , rightMin[MAX]; int lowbit(int x) { return x&(-x); } void update(int x , int y) { while(x <= MAX) { cnt[x] += y; x += lowbit(x); } } int sum(int x) { int sum = 0; while(x) { sum += cnt[x]; x -= lowbit(x); } return sum; } int main() { int t,i,j; scanf("%d" , &t); while(t--) { int n; scanf("%d" , &n); for( i = 1 ; i <= n ; i++) scanf("%d" , &a[i]); memset(cnt , 0 , sizeof(cnt));//清零 for( i = 1 ; i <= n ; i++) { update(a[i] , 1); leftMin[i] = sum(a[i]-1);//计算左边小的个数 leftMax[i] = i - leftMin[i] -1;//计算左边大的个数 // leftMax[i] = sum(MAX) - sum(skill[i]);//计算左边大的个数 } memset(cnt , 0 , sizeof(cnt));//注意此处要再次清零 for( i = n,j = 1 ; i >= 1 ;j++, i--) { update(a[i] , 1); rightMin[i] = sum(a[i]-1);//计算右边小的个数 rightMax[i] = j - rightMin[i]-1;//计算右边大的个数 // rightMax[i] = sum(MAX) - sum(skill[i]);//计算左右大的个数 } __int64 sum = 0; for( i = 1 ; i <= n ; i++) { sum += leftMax[i]*rightMin[i] + leftMin[i]*rightMax[i];//交叉相乘取和 } printf("%I64d\n" , sum); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。