Ping pong
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

 

N(3N20000) 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(1T20) , 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 a1a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1ai100000 , 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

Source

Beijing 2008-2009

枚举裁判的位置+树状数组

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxn=; int tree[maxn+],n; inline int lowbit(int x) {return x&(-x);} void Init()
{
memset(tree,,sizeof(tree));
} int Add(int x)
{
while(x<=maxn)
{
tree[x]++;
x+=lowbit(x);
}
} int getSum(int x)
{
int sum=;
while(x>)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
} struct node
{
int leftbig,leftsmall;
int rightbig,rightsmall;
}ND[]; int bilib[]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int ans=;
scanf("%d",&n);
int x;
for(int i=;i<=n;i++)
{
scanf("%d",&bilib[i]);
}
Init();
for(int i=;i<=n;i++)
{
ND[i].leftsmall=getSum(bilib[i]);
Add(bilib[i]);
ND[i].leftbig=i--ND[i].leftsmall;
}
Init();
for(int i=n;i>=;i--)
{
ND[i].rightsmall=getSum(bilib[i]);
Add(bilib[i]);
ND[i].rightbig=n-i-ND[i].rightsmall;
}
///debug/*
//for(int i=1;i<=n;i++)
//{
// cout<<ND[i].leftsmall<<" , "<<ND[i].leftbig<<endl;
// cout<<ND[i].rightsmall<<" , "<<ND[i].rightbig<<endl;
// cout<<endl;
//}
///.....*/
for(int i=;i<=n-;i++)
{
ans+=1LL*((ND[i].leftsmall*ND[i].rightbig)+(ND[i].leftbig*ND[i].rightsmall));
}
cout<<ans<<endl;
}
return ;
}

UVALive 4329 Ping pong的更多相关文章

  1. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

  2. UVALive 4329 Ping pong(树状数组)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...

  3. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  4. UVALive 4329 Ping pong (BIT)

    枚举中间的人,只要知道在这个人前面的技能值比他小的人数和后面技能值比他小的人数就能计算方案数了,技能值大的可有小的推出. 因此可以利用树状数组,从左到右往树上插点,每个点询问sum(a[i]-1)就是 ...

  5. ACM-ICPC LA 4329 Ping pong(树状数组)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  7. BIT LA 4329 Ping pong

    题目传送门 题意:训练指南P197 分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了 #include <b ...

  8. LA 4329 Ping pong (树状数组)

    题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...

  9. LA 4329 Ping pong

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ; ; ...

随机推荐

  1. C#性能优化考虑的几个方向

    装箱与拆箱 ArrayList's vs. generic List for primitive types and 64-bits 类型转换   GC 注意SOH对象应该较快,避免内存泄漏 注意LO ...

  2. FineReport如何连接和使用MongoDB数据库

    随着NoSQL数据库越来越流行,MongoDB数据库作为NoSQL数据库中的领头羊,使用也越来越广泛.为此,FineReport V8.0版本提供了数据连接和数据集接口,可以通过开发一款可以连接和使用 ...

  3. H5框架之Bootstrap(一)

    H5框架之Bootstrap(一) 接下来的时间里,我将和大家一起对当前非常流行的前端框架Bootstrap进行速度的学习,以案例的形式.对刚开始想学习Bootstrap的同学而找不着边的就很有帮助了 ...

  4. Python3的decode()与encode()

    python3的decode()与encode() Tags: Python Python3 对于从python2.7过来的人,对python3的感受就是python3对文本以及二进制数据做了比较清晰 ...

  5. hbase-site.xml中HBASE_CLASSPATH 的设置

    http://www.dataguru.cn/thread-95064-1-1.html

  6. one to many

    @OneToMany(mappedBy="order",fetch=FetchType.EAGER,cascade={CascadeType.ALL})    private Li ...

  7. __definedGetter\Setter__的一些想法

    __definedGetter\Setter__ 是JS5在创建对象后内置的方法,用于在读写对象属性的时候执行的方法. zhangmingzhi.__defineSetter__('age',func ...

  8. [网络安全] [视频分享]KaLi Linux基础培训2016 最新的哦【福吧资源网】

    最新的教程同时针对kali linux2016最新版本的多个问题解决办法还有一些实例利用. 下载地址:http://www.fu83.cn/thread-310-1-1.html

  9. 给大家分享一个jQuery TAB插件演示

    jquery tab选项卡插件示例页面代码,使用jquery.tabs.js轻量级的tab选项卡插件来实现,并支持鼠标滑过.点击.自动切换.数据回调等功能,有的是点击切换,有的是鼠标滑过切换,自带了多 ...

  10. iOS --- DIY文件名批量修改

    批量修改文件名: // 1.创建文件管理 NSFileManager *filemanager =[NSFileManager defaultManager]; // 2. 获得所有文件夹路径 NSS ...