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. MVC MemeberShip vs. Asp.net Identity

    参考 从Membership 到 .NET4.5 之 ASP.NET Identity Extending Identity Accounts and Implementing Role-Based ...

  2. codevs2178 表达式运算Cuties[笛卡尔树]

    2178 表达式运算Cuties  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 给出一个表达 ...

  3. jquery工具方法swap

    swap : css交换(内部) 详细内容请点击 -> 当元素的样式为display:none时获取他的宽高

  4. IIS上部署Net.Core

    部署: 1.安装vc_redist.x64vc_redist.x64 2.安装DotNetCore.1.0.0.RC2-WindowsHosting 3.安装DotNetCore.1.0.0-SDK. ...

  5. 配置容器configuring Containsers

    容器可以在运行时配置,相反的也可以通过应用程序的配置文件(或扩展配置文件)来配置. Unity的三个高级功能:泛型装饰链.解析器重写和数组注入. 1.配置开放式泛型来解析封闭式泛型 只要不是为封闭型泛 ...

  6. 重写navigationController的push方法后,出现卡顿现象

    在使用navigation的pushViewController进行push的时候,两个页面间的动画会出现卡顿一下再推出的效果,是因为iOS7 viewController背景颜色的问题,看到大神的博 ...

  7. 自己写一个JS单向数据流动库----one way binding

    JS单向流动其实就是数据到视图的过程, 这几天突发奇想,想着弄一个插件, 把DOM结构使用JS进行描述: 因为DOM中的Class , content, id, attribute, 事件, 子元素全 ...

  8. java-json日期字符串转换

    String valueStr = value.toString(); if(StringUtils.isBlank(valueStr) || "null".equals(valu ...

  9. jquery 使用方法

      jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库.对于网页开发者来 ...

  10. bzoj4282慎二的随机数列

    海带头又上线了QwQ~ 这是一个奇怪的lis问题 显然一定存在一种最优答案使所有辨认不清的数都在答案中. [为什么呢]因为你完全可以用一个'N'来替换一个'K'啊QwQ~ 那么在选完所有'N'之后,一 ...