【暑假】[实用数据结构]UVAlive 4329 Ping pong

来源:互联网 发布:淘宝客域名注册 编辑:程序博客网 时间:2024/06/01 09:38
UVAlive 4329 Ping pong

 

题目:

Ping pong
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

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 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$ \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


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:
求一共能组织多少种比赛。如果裁判位于i,那么这种情况可以组织c[i]*(n-i-d[i]) + d[i]*(i-1-c[i])种比赛,其中c[i]为i左边比a[i]小的数目而d[i]为i右边比a[i]小的数目。问题转化为求c[i]与d[i],引入数组x,x[j]表示到目前为止是否有a[i]=j有则为1,那么c[i]就是x[i]的前缀和。d同理。ans需要枚举裁判的位置才能得到,因此需要不断修改x(add目前考虑位置的x),是动态前缀和的问题。代码由FenwickTree实现。

代码:
 1 #include<iostream>  2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<queue> 6 #define FOR(a,b,c) for(int a=(b);a<(c);a++) 7 using namespace std; 8  9 const int maxn = 20000 + 10,INF=1<<30;10 11 inline int lowbit(int u) { return u&-u; }12 13 struct FenwickTree {  //动态范围求和问题 14 int n;15 vector<int> C;  //当数组用 16   17   void resize(int n){ this->n=n;C.resize(n);  } //重新调节大小 18   void clear(){ fill(C.begin(),C.end(),0);    } //清零C 19   20   int sum(int u){21       int ret=0;22       while(u>0){23           ret += C[u]; u -= lowbit(u);24       }25       return ret;26   }27   void add(int u,int d){28       while(u <= n){29           C[u] += d; u += lowbit(u);30       }31   }32 };33 34 FenwickTree f;35 int c[maxn],d[maxn],a[maxn];36 int n;37 38 int main(){39 int T;  scanf("%d",&T);40   while(T--){41       scanf("%d",&n);42       int maxa=-INF;43       FOR(i,1,n+1) {44           scanf("%d",&a[i]);maxa=max(maxa,a[i]);45       }46       f.resize(maxa); //将f.n载为maxa 47       f.clear();      //clear148       FOR(i,1,n+1) {49           f.add(a[i],1); //x[a[i]]=150           c[i]=f.sum(a[i]-1);51           //sum(x,1,a[i]-1)52       }53       f.clear();      //clear2 54     for(int i=n;i;i--){   //注意是--55         f.add(a[i],1);  56         d[i]=f.sum(a[i]-1); 57     }58     59     long long ans=0;60     FOR(i,1,n+1) 61      ans += (long long)c[i]*(n-i-d[i]) + (long long)(i-c[i]-1)*d[i];62     printf("%lld\n",ans); 63   }64   return 0;65 }

 


0 0
原创粉丝点击