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. Font: a C++ class

    Font: a C++ class        This class is used in Fractal Generator.    Avi Examples The header fileFon ...

  2. JAVA垃圾回收机

    垃圾回收基本算法 串型回收和并行回收 串行回收始终在一个CPU上执行回收操作.并行回收则将回收任务分为好几步,每步使用不同的CPU执行,这样加快了执行速度,有点像流水线作业. 并发执行和暂停应用程序 ...

  3. 20145316《Java程序设计》第十周学习总结

    学习内容总结 网络编程 1.网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 2.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴. 3.在发 ...

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

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

  5. Linux学习笔记之Linux修改或增加ssh端口

    1.什么是SSH SSH 为 Secure Shell 由 IETF 的网络工作小组(Network Working Group)所制定: SSH 是建立在应用层和传输层基础上的一种安全协议. SSH ...

  6. PHP+MySQL数据库编程的步骤

    第一步:PHP连接MySQL服务器 第二步:选择当前要操作的数据库 第三步:设置请求或返回的数据的字符集 第四步:执行各种SQL语句. PHP连接MySQL服务器 1.mysql_connect() ...

  7. 20145322何志威 《Java程序设计》课程总结

    课程总结 每周读书笔记链接汇总 •第一周读书笔记 •第二周读书笔记 •第三周读书笔记 •第四周读书笔记 •第五周读书笔记 •第六周读书笔记 •第七周读书笔记 •第八周读书笔记 •第九周读书笔记 •第十 ...

  8. 4.9版本的linux内核中实时时钟芯片pt7c4338的驱动源码在哪里

    答:drivers/rtc/rtc-ds1307.c,内核配置项为CONFIG_RTC_DRV_DS1307 Location: -> Device Drivers -> Real Tim ...

  9. java堆排序(大根堆)

    实现堆排序的算法思路是先创建堆,也就是从叶子节点起对每一层的孩子节点及其对应位置的父亲节点进行比较,较大的孩子节点替换较小的父亲节点,一级一级比较替换,就创建出了大根堆,小根堆反之.创建好大根堆以后, ...

  10. Extjs前端框架解决了什么问题

    Extjs 作为一套企业级富客户端前端开发框架,主要解决了以下问题: 1.DOM Ext.Element: Ext.Element.get()快捷方式Ext.get(),只能以dom的id作为参数去获 ...