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).
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
题意:
英语差所以一直搞不懂题意,之后问了个英语大神题意,他说找到一个数的左边比它大的个数与左边比它小的个数,然后再右边比它大的个数与右边比它小的个数。然后裁判是要左小+右大或左大右小的才能做。思路:这题很明显是用树状数组做的,逆序数求出上述的个数,而这里很难也很是复杂。放了这题一个多月重新再拿出来做终于A了。这题挺坑的左大与左小竟然能在同一个数组里弄,而右大与右小又必须分开弄。还有就是逆序数的是向上取和还是向下取和的问题与Add()向上增加还是向下增加搞到头都大了。卡题数据(对了基本能A):19 4 12 13 34 1 1 4 8 21 12 10 1 4 10 12 21 8 1 4 1 21 2 4 1 4 4 3 2 1 4 4 2 1 4 3 0 4 1 2 3 4 4 3 3 2 1 1 3 1 2 3
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; #define T 100005 typedef long long ll; #define CRL(a) memset(a,0,sizeof(a)) ll a[T],c1[T],c2[T],c3[T],c4[T],t[T]; int lowbit(int x) { return x&(-x); } int Sum(int n,int i) { int sum=0; if(i==1) while(n<T) { sum+=t[n]; n+=lowbit(n); } else while(n>0) { sum+=t[n]; n-=lowbit(n); } return sum; } void Add(int n,int i) { if(i==1) while(n<T) { t[n]++; n+=lowbit(n); } else while(n>0) { t[n]++; n-=lowbit(n); } } int main() { /*freopen("input.txt","r",stdin);*/ int n,m,i;ll k; scanf("%d",&n); while(n--) { scanf("%d",&m); for(i=1;i<=m;++i){ scanf("%lld",&a[i]);//Sum(,-1)向下取区间 c1[i] = Sum(a[i]-1,-1);Add(a[i],1);//左边比自己小的 if(i-1-c1[i]>0)c3[i]=i-1-c1[i];//左边比自己大的 }CRL(t); //for(i=1;i<=m;++i){ // if(i-1-c1[i]>0)c3[i]=i-1-c1[i];//左边比自己大的 //} /*for(i=1;i<=m;++i)printf("%d ",c1[i]);printf("\n"); for(i=1;i<=m;++i)printf("%d ",c3[i]);printf("\n");*/ for(i=m;i>0;--i){//Sum(,-1)向上取区间 c2[i] = Sum(a[i]+1,1);Add(a[i],-1);//右边比自己大的 //if(i-1-c2[i]>0)c4[m-i+1]=i-1-c2[i];//右边比自己小的 }CRL(t);k=0; for(i=m;i>0;--i) if(i-1-c2[m-i+1]>0)c4[m-i+1]=i-1-c2[m-i+1];//右边比自己小的 /* for(i=1;i<=m;++i)printf("%d ",c2[i]);printf("\n"); for(i=1;i<=m;++i)printf("%d ",c4[i]);printf("\n");*/ for(i=2;i<=m-1;++i){ k+=c1[i]*c2[i]+c4[i]*c3[i]; } CRL(c1),CRL(c2);CRL(c3),CRL(c4); printf("%lld\n",k); } return 0; }