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. 5.MySQL必知必会之过滤数据-WHERE

    本章将讲授如何使用SELECT语句的WHERE子句指定搜索条件. 1.使用WHERE子句 数据库表一般包含大量的数据,很少需要检索表中所有行.通常只 会根据特定操作或报告的需要提取表数据的子集.只检索 ...

  2. javascript 对象 原型 prototype

  3. HDU 4585 Shaolin(map应用+二分)

    题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进 ...

  4. Winter-2-STL-A Argus 解题报告及测试数据

    Time Limit:2000MS     Memory Limit:65536KB Description A data stream is a real-time, continuous, ord ...

  5. Jmeter数据库mysql测试说明

    主要分3个步骤,详细操作步骤如下: 一.环境准备 1.下载mysql驱动,下载地址:https://dev.mysql.com/downloads/connector/j/,Select Operat ...

  6. windows7上安装php7和apche2.4

    windows7在配置php7+apache2.4 1.下载并安装vc14http://www.microsoft.com/zh-cn/download/details.aspx?id=48145下载 ...

  7. 对于JVM中方法区,永久代,元空间以及字符串常量池的迁移和string.intern方法

    在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法信息(如字节码,栈和变量大小),运行时常量池,已确定的符号引用和虚方法表. 在过去(当自定义类加载器使用不普 ...

  8. 如何用纯 CSS 创作一个单元素抛盒子的 loader

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKwXbx 可交互视频 ...

  9. KALI视频学习11-15

    KALI视频学习11-15 第十一集 看到openvas的主界面(web界面) ping靶机,看是否能正常连通 创建一个扫描目标Configuration-Targets,默认扫描目标为本机 添加一个 ...

  10. shell编程(二)

    case判断 前面了解了shell编程的if判断,其实除了if判断,还有case判断. case语法: case VAR in case1) command1 ;; case2) command2 ; ...