首页 技术 正文
技术 2022年11月20日
0 收藏 625 点赞 5,035 浏览 2304 个字

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

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
题意:见白书p197 ,注意没有两个人的rank是一样的
思路:利用树状数组,树状数组教程(盗链)http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html
考虑第i个人当裁判的情形。假设i左边有ci个比ai小,那么就有(i-1)-ci个比ai大;
同理:假设右边有di个比ai小,那么就有(n-i)-di个比ai大。所以当i当裁判时,有ci(n-i-di)+(i-ci-1)di种比赛。
建立一个大小<=max(ai)的树状数组,全赋0。从左到右扫描数轴,扫描到第i个人时,add(a[i],1)。我们可以发现,此时在ci的值即为sum(a[i]-1)。因为以后这个值会发生变化,所以我们用一个数组把它存起来。全部扫完一遍之后,我们又可以发现,di的值为sum(a[i]-1)-ci。求解结束。
 /*
* Author: Joshua
* Created Time: 2014年07月13日 星期日 14时09分45秒
* File Name: poj3928.cpp
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; #define maxv 100005
#define maxn 20005
typedef long long LL; int c[maxv],n,vv;
int lowBit(int x)
{
return x&(-x);
} void add(int x,int v)
{
while (x<=vv)
{
c[x]+=v;
x+=lowBit(x);
}
} int sum(int x)
{
int ret=;
while (x>)
{
ret+=c[x];
x-=lowBit(x);
}
return ret;
} void solve()
{
int a[maxn],temp[maxv];
LL ans=;
memset(c,,sizeof(c));
scanf("%d",&n);
vv=;
for (int i=;i<=n;++i)
{
scanf("%d",&a[i]);
vv=max(vv,a[i]);
}
for (int i=;i<=n;++i)
{
add(a[i],);
temp[i]=sum(a[i]-);
}
for (int i=;i<=n;++i)
{
int ts=sum(a[i]-);
ans+=temp[i]*(n-i-(ts-temp[i]));
ans+=(i--temp[i])*(ts-temp[i]);
}
printf("%lld\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while (T)
{
solve();
T--;
}
return ;
}
     
上一篇: K好数--蓝桥杯
下一篇: OC
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,103
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,579
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,427
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,199
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,834
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,917