首页 > 代码库 > POJ3928 Ping pong
POJ3928 Ping pong
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
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
Source
Beijing 2008
枚举每一个位置x作为裁判,ans+=(x左边能力值比x小的人数)*(x右边能力值比x大的人数)+(x右边能力值比x小的人数)*(x左边能力值比x大的人数)
将读入的每个能力值记录id,按能力值从小到大排序,用树状数组维护上述人数并计算答案即可
极限情况下答案为C(n,3),超过int范围,需要long long
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 using namespace std; 9 const int mxn=100020;10 int read(){11 int x=0,f=1;char ch=getchar();12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}14 return x*f;15 }16 int n;17 LL t[mxn+5];18 struct node{19 int x;20 int id;21 }a[mxn];22 int cmp(const node a,const node b){23 return a.x<b.x;24 }25 int lowbit(int x){return x&-x;}26 void add(int p,int v){//k==0 个数 k==1 距离 27 while(p<=mxn){t[p]+=v;p+=lowbit(p);}28 }29 LL smm(int x){30 LL res=0;31 while(x){res+=t[x];x-=lowbit(x);}32 return res;33 }34 int T;35 int main(){36 scanf("%d",&T);37 int i,j;38 while(T--){39 memset(t,0,sizeof t);40 scanf("%d",&n);41 for(i=1;i<=n;i++){42 a[i].x=read();43 a[i].id=i;44 }45 LL ans=0;46 sort(a+1,a+n+1,cmp);47 add(a[1].id,1);48 for(i=2;i<n;i++){49 int ldn=smm(a[i].id);//所有id小于当前id,且x小于当前x的 50 int lup=a[i].id-1-ldn;//所有id小于当前id,但x大于当前x的51 int rdn=(smm(mxn)-ldn);//所有id大于当前id,且x小于当前x的 52 int rup=n-rdn-a[i].id;//所有id大于当前id,且x大于当前x的53 ans+=ldn*rup+lup*rdn;54 add(a[i].id,1);55 }56 printf("%lld\n",ans);57 }58 return 0;59 }
POJ3928 Ping pong
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。