博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA - 1428 Ping pong
阅读量:6958 次
发布时间:2019-06-27

本文共 2831 字,大约阅读时间需要 9 分钟。

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).

Output

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

 

题解:

  树状数组裸题吧,考虑维护c[i],表示在a[i]之前有多少人rank比a[i]小,d[i]表示a[i]之后有多少人rank比i小,考虑枚举中点i,那么对于一个裁判i对答案的贡献就显然ans+=(i-1-c[i])*d[i]+(n-i-d[i])*c[i]。(i-1-c[i]表示i之前比a[i]大的人数,n-i-d[i]同理)。那么答案就将所有的加起来就可以了,用一棵值域树状数组就可以nlogn求了。

 

代码:

#include
#include
#include
#include
#include
#define MAXN 100100#define ll long longusing namespace std;int a[MAXN],tr[MAXN],c[MAXN],d[MAXN];int n,t,maxx;int lb(int x){ return x&(-x);}int query(int x){ int ret=0; while(x){ ret+=tr[x]; x-=lb(x); } return ret;}void insert(int ps,int h){ while(ps<=maxx){ tr[ps]+=h; ps+=lb(ps); }}int main(){ scanf("%d",&t); while(t--){ memset(a,0,sizeof(a)); memset(tr,0,sizeof(tr)); memset(c,0,sizeof(c)); memset(d,0,sizeof(d));maxx=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),maxx=max(maxx,a[i]); for(int i=1;i<=n;i++){ c[i]=query(a[i]-1); insert(a[i],1); } memset(tr,0,sizeof(tr)); for(int i=n;i>=1;i--){ d[i]=query(a[i]-1); insert(a[i],1); } ll ans=0; for(int i=1;i<=n;i++){ ans+=(ll)(i-1-c[i])*(ll)d[i]; ans+=(ll)(n-i-d[i])*(ll)c[i]; } printf("%lld\n",ans); }}

 

转载于:https://www.cnblogs.com/renjianshige/p/7470790.html

你可能感兴趣的文章
设计模式学习总结-桥接模式(Bridge Pattern)
查看>>
halcon算子翻译——copy_image
查看>>
使用Haar分类器进行面部检测
查看>>
Makefile-2
查看>>
获取页面中出现次数最多的三个标签以及出现次数
查看>>
访问WEB-INF目录中的文件
查看>>
web接口开发与测试
查看>>
php -- php控制linux关机、重启、注销
查看>>
十.python面向对象(itme)
查看>>
Python下selenium的简单用法
查看>>
multiset的应用
查看>>
我的mysql的学习记录
查看>>
Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
查看>>
NYOJ 题目77 开灯问题(简单模拟)
查看>>
模式识别
查看>>
设置文本编辑器的按回车时触发的事件
查看>>
关于android手机不能打印Log日志
查看>>
hdu 2157 How many ways?? **
查看>>
xcode8 更新cocoapods
查看>>
习题:海盗船(广搜)
查看>>