【UVA1428】Ping pong
2017-10-15 15:37
330 查看
题面
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?
题意
有n个人,每个人有一个权值Wi,Wi互不相同
一个三元组(a,b,c)能够产生1的贡献当且仅当a<b<c,并且Wa<Wb<Wc或者Wa>Wb>Wc
问这n个人能产生多大的贡献(a,b,c和c,b,a算同一个三元组)
解法
线段树:
易知,对于i来说,设i左边的人的W<Wi的人数为Ci,右边的为Di,那么答案就是∑ni=1Di∗(i−Ci−1)+Ci(n−i−Di)
现在问题就变成了怎么求Ci和Di。可以看到,C与D的值取决于权值小于W的个数,所以可以种两棵权值线段树,一棵从前往后递推求C,另一棵从后往前递推求D,然后再利用上面的公式求答案即可
复杂度
O(nlogn)
代码
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?
题意
有n个人,每个人有一个权值Wi,Wi互不相同
一个三元组(a,b,c)能够产生1的贡献当且仅当a<b<c,并且Wa<Wb<Wc或者Wa>Wb>Wc
问这n个人能产生多大的贡献(a,b,c和c,b,a算同一个三元组)
解法
线段树:
易知,对于i来说,设i左边的人的W<Wi的人数为Ci,右边的为Di,那么答案就是∑ni=1Di∗(i−Ci−1)+Ci(n−i−Di)
现在问题就变成了怎么求Ci和Di。可以看到,C与D的值取决于权值小于W的个数,所以可以种两棵权值线段树,一棵从前往后递推求C,另一棵从后往前递推求D,然后再利用上面的公式求答案即可
复杂度
O(nlogn)
代码
#include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #define ls 2*k #define rs 2*k+1 #define Lint long long int using namespace std; const int N=20010; const int R=100000; struct node { int l,r; int sum; void clear() { sum=0; } }t[R*5],p[R*5]; int c ,d ,w ; int T,n; Lint ans; void build(int k,int l,int r) { p[k].l=t[k].l=l,p[k].r=t[k].r=r; if( l==r ) return ; int mid=(l+r)/2; build( ls,l,mid ),build( rs,mid+1,r ); } void insert_t(int k,int x) { if( t[k].l==t[k].r ) { t[k].sum=1;return ; } if( x<=t[ls].r ) insert_t( ls,x ); else insert_t( rs,x ); t[k].sum=t[ls].sum+t[rs].sum; } void insert_p(int k,int x) { if( p[k].l==p[k].r ) { p[k].sum=1;return ; } if( x<=p[ls].r ) insert_p( ls,x ); else insert_p( rs,x ); p[k].sum=p[ls].sum+p[rs].sum; } int query_t(int k,int l,int r) { if( t[k].l==l && t[k].r==r ) return t[k].sum; if( r<=t[ls].r ) return query_t( ls,l,r ); else if( l>=t[rs].l ) return query_t( rs,l,r ); else return query_t( ls,l,t[ls].r )+query_t( rs,t[rs].l,r ); } int query_p(int k,int l,int r) { if( p[k].l==l && p[k].r==r ) return p[k].sum; if( r<=p[ls].r ) return query_p( ls,l,r ); else if( l>=p[rs].l ) return query_p( rs,l,r ); else return query_p( ls,l,p[ls].r )+query_p( rs,p[rs].l,r ); } int main() { build( 1,0,R ); scanf("%d",&T); while( T-- ) { scanf("%d",&n); ans=0; for(int i=1;i<=n;i++) { c[i]=d[i]=0; scanf("%d",&w[i]); } for(int i=1;i<=R*4;i++) p[i].clear(),t[i].clear(); for(int i=1;i<=n;i++) { c[i]=query_t( 1,0,w[i]-1 ); insert_t( 1,w[i] ); } for(int i=n;i>=1;i--) { d[i]=query_p( 1,0,w[i]-1 ); insert_p( 1,w[i] ); } for(int i=1;i<=n;i++) ans=ans+(Lint)d[i]*(i-c[i]-1)+(Lint)c[i]*(n-i-d[i]); printf("%lld\n",ans); } return 0; }
相关文章推荐
- uva 1428 Ping pong (树状数组)
- UVa 1428 - Ping pong
- uva 1428 - Ping pong
- UVA 1428 - Ping pong(树状数组)
- uva 1428 - Ping pong(树状数组)
- Uva 1428 Ping pong (树状数组,Fenwick树)
- uva 1428 - Ping pong
- uva 1428 - Ping pong (树状数组的应用)
- UVA 1428 - Ping pong(树状数组)
- UVA 1428 Ping Pong(树状数组)
- [树状数组]Ping Pong, Beijing 2008, Uva1428
- UVA-1428 - Ping pong(树状数组)
- UVA1428 Ping pong(树状数组)
- uva 1428 - Ping pong
- uva 1428 - Ping pong(树状数组,4级)
- UVa 1428 - Ping pong
- uva 1428 - Ping pong (二叉索引树)
- UVA 1428 - Ping pong(树状数组)
- uva 1428 - Ping pong(树状数组,4级)
- 树状数组(二叉索引树)(uva 1428 - Ping pong )