Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1143    Accepted Submission(s): 387

Problem 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
lrj蓝书上BIT的课后习题,挺不错的。
题目读起来有点略拗口,意思就是有N的人,每个人有唯一的一个技能值,规定想要组一场比赛的话需要三个人,且其中一个人做裁判,裁判的技能值必须位于剩余二人之间且裁判的位置也必须位于二者之间,问有多少不同的比赛阵容,输入时按照位置从左至右输入所有人的技能值。
我们把每个人考虑做裁判的方案数,然后累加起来,假设ai做裁判,如果我们知道ai前面分数小于它的人数ci和其后面分数小于它的人数di,
则ai做裁判有 ( ci*(N-i-di) + di*(i-1-ci) )方案数,所以问题转化为求ci和di。
观察得知分数最大10w并不是很大,考虑将分数作为线段树的区间,保存的是区间分数的总人数,然后一边更新一边记录ci,di可以清空树以后再逆序插入更新。
区间和!BIT!简单高效!
 #include<bits/stdc++.h>
using namespace std;
#define LL long long
int sumv[+];
int c[],d[],a[];
int lowbit(int x){return x&-x;}
int sum(int x)
{
int ret=;
while(x){
ret+=sumv[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=){
sumv[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int N,t,m,i,j,k;
scanf("%d",&t);
while(t--){memset(sumv,,sizeof(sumv));
LL ans=;
scanf("%d",&N);
for(i=;i<=N;++i){
scanf("%d",&a[i]);
add(a[i],);
c[i]=sum(a[i]-);
}memset(sumv,,sizeof(sumv));
for(i=N;i>=;--i){
add(a[i],);
d[i]=sum(a[i]-);
}
for(i=;i<=N;++i){
ans+=(LL)c[i]*(N-i-d[i])+d[i]*(i--c[i]);
}
printf("%lld\n",ans);
}
return ;
}

HDU 2492 BIT/逆序数/排列组合的更多相关文章

  1. HDU 5816 状压DP&排列组合

    ---恢复内容开始--- Hearthstone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  2. HDU 1261 字串数(排列组合)

    字串数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  4. hdu 1394(线段树) 最小逆序数

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...

  5. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  6. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  7. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. 排列组合+组合数取模 HDU 5894

    // 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...

  9. [HDU POJ] 逆序数

    HDU 1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

随机推荐

  1. hadoop开发setjar方法

    屏蔽 //job.setJar("/Users/sumeng/IdeaProjects/cloudHadoop/out/artifacts/cloudHadoop_jar/cloudHado ...

  2. 【android】通过leakCanary找出程序内存泄露点

    背景 内存泄露是咱新手比较头痛的问题,因为它不像崩溃,在开发环境可以根据提示的错误信息排查问题. 你都不知道咱的app是否哪个犄角旮旯藏着一个吞噬内存的黑洞. 排查android 内存泄露比较底层高端 ...

  3. quartz (二) Spring+Quartz实现定时任务的配置方法

    JobDetail  设置执行的任务 :CronTrigger   触发器:设置执行的时间规则      ;  Scheduler    // 调度器,将任务与执行时间关联 本文转自:http://w ...

  4. templates页面超链接访问Controller方法

    <h3>部署列表:</h3> <table border="1px" cellspacing="0px" width=" ...

  5. 在VS2012中采用C++中调用DLL中的函数(4)

    转自:http://www.cnblogs.com/woshitianma/p/3683495.html 这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天 ...

  6. Centos75 解决Nginx出现403 forbidden(13: Permission denied)

    Centos75 新安装的vm,nginx出现403 forbidden 一般为SELinux设置为开启状态(enabled)的原因 切为root ,执行: sed -i 's/SELINUX=enf ...

  7. Java学习第三周摘要

    20145307<Java程序设计>第三周学习总结 教材学习内容总结 认识对象 类类型 Java可区分为基本类型和类类型两大类型系统,其中类类型也称为参考类型.sun就是一个类类型变量,类 ...

  8. MR案例:内连接代码实现

    本文是对Hive中[内连接]的Java-API的实现,具体的HQL语句详见Hive查询Join package join.map; import java.io.IOException; import ...

  9. [BZOJ1576]安全路经Travel

    题目大意:从1号点出发,到每个点的最短路的最后一条边不能被访问,求此时1号点到其他点的最短路 建立最短路树,对于一条非树边,把它加进去会形成一个环和一条链,如图: 即红色和蓝色路径构成的图,它的长度为 ...

  10. iOS开发进阶 - 自定义UICollectionViewLayout实现瀑布流布局

    移动端访问不佳,请访问我的个人博客 最近项目中需要用到瀑布流的效果,但是用UICollectionViewFlowLayout又达不到效果,自己动手写了一个瀑布流的layout,下面是我的心路路程 先 ...