LA 4329(树状数组)
题目描述:
N <tex2html_verbatim_mark>(3
N
20000) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>(1
T
20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.
Every test case consists of N + 1 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1, a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1
ai
100000 <tex2html_verbatim_mark>, i = 1...N <tex2html_verbatim_mark>).
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 分析:对于每个a[i],算出从a[1]到a[i-1]比a[i]小的数有c[i]个,a[i+1]到a[n]比a[i]小的有d[i]个,则考虑当i为裁判时,可能的方案数为:c[i](n-i-d[i])+d[i](i-c[i]-1)。关键是算c[i]、d[i]。运用树状数组可以算出c[i],d[i]。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
const int maxn=;
const int maxv=;
int a[maxn],c[maxv],cc[maxn],d[maxn],n;
int lowbit(int x){
return x&-x;
}
int sum(int x){
int res=;
while(x>){
res+=c[x];
x-=lowbit(x);
}
return res;
}
void add(int x,int v){
while(x<=maxv){
c[x]+=v;
x+=lowbit(x);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n-;i++){
cc[i]=sum(a[i]-);
add(a[i],);
}
memset(c,,sizeof(c));
for(int i=n;i>=;i--){
d[i]=sum(a[i]-);
add(a[i],);
}
ll ans=;
for(int i=;i<=n-;i++)
ans+=cc[i]*(n-i-d[i])+(i-cc[i]-)*d[i];
printf("%lld\n",ans);
}
return ;
}
LA 4329(树状数组)的更多相关文章
- LA 4329 (树状数组) Ping pong
第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...
- poj Ping pong LA 4329 (树状数组统计数目)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 879 Descript ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
- UVALive 4329 树状数组第二题
大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...
- TTTTTTTTTTTTT LA 2191 树状数组 稍修改
题意:给出n个数字,操作有修改(S)和输出区间和(M). #include <iostream> #include <cstdio> #include <cstring& ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA 4329 ping-pong树状数组
题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...
- 树状数组 LA 4329 亚洲赛北京赛区题
复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...
- LA 4329(树状数组)
算法竞赛入门经典 p197 题目大意: 一条大街上住着n个乒乓球爱好者.常常比赛切磋技术.每一个人都有一个不同的技能值a[i].每场比赛须要3个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在 ...
随机推荐
- statspack系列3
原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-3/ 作者:Jonathan Lewis 下面的例子中的结果并 ...
- nohup.out
nohup.out 文件的产生 linux的nohup命令的用法 不输出nohup.out nohup node app.js > /dev/null 2>&1 &
- android基本的数据库创建和使用
android的四大组件中就有Content Provider,对其他应用,提供自己的数据,所以,一般情况下,android应用不需要提供content provider. 1. 简单的数据库表单字 ...
- 如用使用高版本framework,比如支持iOS5及以上的工程中使用Social.framework
在Targets -> Build Phases里的 Link Binary With Libraries项中,将高版本的framework,如Social.framework 设置成Optio ...
- [PeterDLax著泛函分析习题参考解答]第3章 Hahn-Banach 定理
1. 证明 $(10'$). 证明: $\ra$: 由 $p_K(x)<1$ 知 $$\bex \exists\ 0<a<1,\st \cfrac{x}{a}\in K. \eex$ ...
- HDU-2975 Billboard
Billboard Time Limit : 20000/8000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- vi同类品
nvi 更纯正,接近原始的vi command argument-F don't copy whole file, may faster-S secure help (quick refe ...
- JavaScript高级程序设计42.pdf
IE中的事件对象 要访问IE中的event对象有几种不同的方式,取决于指定事件处理程序的方法.在使用DOM0级方法添加事件处理程序时,event对象作为window对象的一个属性存在 wondow.e ...
- gcc “-I”(大写i),“-L”(大写l),“-l”(小写l)的区别
我们用gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数,下面做个记录: 例: gcc -o hello hello.c -I /home/hello/inc ...
- 解决sublime联网失败,点击package control后没有反应
在中国大陆有可能出现这种情况,这是因为国内不支持https访问引起的(再次吐槽万恶的GFW ). 这时只需要"preferences -> package settings -> ...