HDU 2492 Ping pong (数状数组)
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?
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
Recommend
gaojie
题目大意:每一个人都有一个实力值,顺序就是他的位置,要求寻找一个对手,然后再寻找一个裁判,要求裁判的实力再他们之间,而且位置要在他们两人的中间,这样可以组成一场比赛。问总共可以组织多少场比赛?
解题思路:以自己为裁判,然后找左边有多少人比较小,再找右边有多少人比自己大,然后两个数相乘就是以他为裁判的比赛场数,当然还有相反的,找右边比自己小的,左边比自己大的,再相乘相加。这样子就变成了:和找逆序数、顺序数差不多了,找左边(右边)比自己大(小)有多少个数,用树状数组最好最快!所以两个for循环就全部找到,然后相乘相加,
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=100010; int n,arr[N+10],a[N+10]; int lmin[N+10],lmax[N+10],rmin[N+10],rmax[N+10]; 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=0; while(i>0){ 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,0,sizeof(arr)); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++){ update(a[i],1); lmin[i]=Sum(a[i]-1); //求左边有多少个数比自己小 lmax[i]=Sum(N)-Sum(a[i]); //求左边有多少个数比自己大 } memset(arr,0,sizeof(arr)); for(int i=n;i>=1;i--){ update(a[i],1); rmin[i]=Sum(a[i]-1); //求右边有多少个数比自己小 rmax[i]=Sum(N)-Sum(a[i]); //求右边有多少个数比自己大 } long long ans=0; for(int i=1;i<=n;i++) ans+=lmin[i]*rmax[i]+lmax[i]*rmin[i]; //1.左边比自己大的数*右边比自己小的数 //2.左边比自己小的数*右边比自己大的数 ,相加就是总和 cout<<ans<<endl; } return 0; }