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. 如何用meavn构建mahout项目

    (1)下载meavn  解压到D盘

  2. 函数对象[条款18]---《C++必知必会》

    有时需要一些行为类似于函数指针的东西,但函数指针显得笨拙.危险而且过时(让我们承认这一点).通常最佳方式是使用函数对象(function object)取代函数指针. 与智能指针一样,函数对象也是一个 ...

  3. 受限的用户shell环境

    有些特殊情况下需要实现将系统内普通用户限定在指定目录下,并且只能使用系统管理员设定的命令.lshell就是实现这样功能的一个神器. lshell提供了一个针对每个用户可配置的限制性shell,lshe ...

  4. (译)Windows Azure的7月更新:SQL数据库,流量管理,自动缩放,虚拟机

    Windows Azure的7月更新:SQL数据库,流量管理,自动缩放,虚拟机 今早我们释出一些很棒的Windows Azure更新.这些新的提升包括:SQL数据库:支持SQL自动导出和一个新的高级层 ...

  5. shell 脚本中双引号 单引号 反引号 的区别

    转自:http://blog.csdn.net/iamlaosong/article/details/54728393 最近要编个shell脚本处理数据,需要检测数据文件是否存在,文件名中包含日期,所 ...

  6. Ubuntu 16.04 安装Postman

    Ubuntu 16.04 安装Postman: 1.官网下载地址:https://www.getpostman.com/根据机器类型选择64位下载. 2.进入下载目录,解压该文件sudo tar -x ...

  7. STM32-串行SPI nor

    源:FLASH 存储学习-串行SPI nor 1.1 SST25VF080B简介1.1.1 主要特性 关键点:容量.速度(时钟速度.读写速度).功耗. l 容量:8MBit: l 最高SPI时钟频率: ...

  8. 一键安装lnmp(2)

    all(){path=`pwd`cd $pathechoecho "exclude=*.i386 *.i686" >> /etc/yum.confrpm -ivh ht ...

  9. encoder-decoder环境部署问题

    pip -v    2.7 cp -r pip2.7 pip pip list appdirs (1.4.3)cycler (0.10.0)distribute (0.7.3)extern (0.1. ...

  10. 20145324 Java实验二

    实验1: 先建立.java 在建立test 测试正常情况 测试出错情况 错误 修改后的代码 正确 测试边界情况 100时出错 修改后的代码 测试 实验2: 先建test在写,java 实验3:建模 练 ...