【UVA1428】Ping pong
来源:互联网 发布:伺服电机编程 编辑:程序博客网 时间:2024/06/07 06:43
题面
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 intusing 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[N],d[N],w[N];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;}
- UVa1428 - Ping pong
- uva1428 Ping pong
- 【UVA1428】Ping pong
- UVA1428 Ping pong(树状数组)
- UVA1428 - Ping pong(树状数组)
- UVA1428 Ping pong(树状数组)
- UVA1428 Ping pong(树状数组)
- Ping pong
- Ping pong
- Ping pong
- Ping pong
- Ping pong
- Ping pong
- Ping pong
- ping-pong
- Ping pong
- hdu Ping pong Ping pong 线段树
- Playing ping-pong
- Java
- Java 打印流PrintStream
- for编写输出等腰三角形,菱形
- Maven的常用命令
- 连续总结第四天
- 【UVA1428】Ping pong
- 文章标题
- Tomcat优化
- Apache Mahout:适合所有人的可扩展机器学习框架
- 第3周第1次课 线性表(3)
- 虚拟机遇到的问题
- 个人总结5
- Java 打印流PrintWriter
- 课堂练习4(第四周)