uvalive 4329

来源:互联网 发布:mac无法读取移动硬盘 编辑:程序博客网 时间:2024/05/10 15:35


Description

Download as PDF

N(3$ \le$N$ \le$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$ \le$T$ \le$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 isN , 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$ \le$ai$ \le$100000 , i = 1...N ).

Output

For each test case, output a single line contains an integer, the total number of different games.

Sample Input

13 1 2 3

Sample Output

1
题意:有n个运动员,有一定顺序,每个人都有自己的排名,三个人位置升序的人组成一场比赛,中间的那个人作为裁判,并且裁判的排名要在其他两位选手之间,问最多能举行多少场比赛(有一个人不一样便视为不同的比赛)
思路:如果枚举运动员的话,时间复杂度较高,不可取,我们这里枚举每一个人作为裁判,运用树状数组计算出裁判两边排名高与和低与裁判排名的人数,这是树状数组的一个常见的使用情况,计算逆序数。
#include <cstring>#include <cstdio>using namespace std;int a[100006];int lmin[100006],rmin[100006];int c[100006];int low(int x){return x&(-x);}int add(int pos){int tm=pos;while(tm<100006){c[tm]+=1;tm+=low(tm);}return 1;}int sum(int pos){int su=0,tmp=pos;while(tmp>0){su+=c[tmp];tmp-=low(tmp);}return su;}int main(){//freopen("in.txt","r",stdin);int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);memset(c,0,sizeof(c));memset(lmin,0,sizeof(lmin));memset(rmin,0,sizeof(rmin));memset(a,0,sizeof(a));for(int i=1;i<=n;i++){scanf("%d",&a[i]);lmin[i]+=sum(a[i]);            add(a[i]);}memset(c,0,sizeof(c));for(int i=n;i>0;i--){rmin[i]+=sum(a[i]);add(a[i]);}long long ans=0;for(int i=2;i<n;i++){ans+=(long long)lmin[i]*(n-i-rmin[i]);   //注意这里有两种情况ans+=(long long)rmin[i]*(i-1-lmin[i]);}printf("%lld\n",ans);}}

0 0
原创粉丝点击