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.

题目大意:有一个互不相等的序列,问有多少个ai、aj、ak满足i<j<k并且ai<aj<ak或者ai>aj>ak。

思路:考虑i<j<k并且ai<aj<ak的,先找出所有的i<j并且ai<aj,令sum[j]为满足i<j并且ai<aj的对数。然后就是枚举k,找出所有比k小的j并把sum[j]加起来。这种合法三元组最多C(2W,3)还是蛮大的,要用64位整数。反面一样。用树状数组就可以解决了。详细看代码吧这个不大会讲。

思路2:其实有个更简单的办法,只是我不久前受到了树状数组求逆序对的影响(已经过了很久了好不好!)。就是对每一个数x,找出左边比它小的数一共x个,右边比它大的数一共y个,那么中间为x的数的逆序对就有xy个,反面一样。这个没有代码我懒得写,DISCUSS有自己去看……

PS:之前做过一条codeforces的61E,也是差不多的题目,不过那题要离散化,这题大概不用也能过(我顺手离散化了……)。我记忆力渣渣以为上次用的是32位这次也用了int结果WA了,原来上次用的也是64位……以后还是算一下别相信记忆力了……

PS2:样例好坑爹啊输出1,害我要自己设计数据验证数得好辛苦啊……

代码(281MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXN = ; struct Node {
int val, id;
bool operator < (const Node &rhs) const {
return val < rhs.val;
}
}; Node a[MAXN];
LL tree[MAXN], sum[MAXN];
int f[MAXN];
int n; inline int lowbit(int x) {
return x & -x;
} inline LL get_sum(int k) {
LL ret = ;
while(k > ) {
ret += tree[k];
k -= lowbit(k);
}
return ret;
} inline void modify(int k, LL x) {
while(k <= n) {
tree[k] += x;
k += lowbit(k);
}
} LL solve() {
memset(tree, , sizeof(tree));
LL ret = ;
for(int i = ; i <= n; ++i) {
sum[i] = get_sum(f[i]);
modify(f[i], );
}
memset(tree, , sizeof(tree));
for(int i = ; i <= n; ++i) {
ret += get_sum(f[i]);
modify(f[i], sum[i]);
}
memset(tree, , sizeof(tree));
for(int i = n; i > ; --i) {
sum[i] = get_sum(f[i]);
modify(f[i], );
}
memset(tree, , sizeof(tree));
for(int i = n; i > ; --i) {
ret += get_sum(f[i]);
modify(f[i], sum[i]);
}
return ret;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &a[i].val), a[i].id = i;
sort(a + , a + n + );
for(int i = ; i <= n; ++i) f[a[i].id] = i;
//printf("%d\n", solve());
cout<<solve()<<endl;
}
}

HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)的更多相关文章

  1. HDU 2492 Ping pong (数状数组)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. Ping pong(树状数组求序列中比某个位置上的数小的数字个数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others) ...

  3. POJ 3928 Ping pong(树状数组)

                                                                          Ping pong Time Limit: 1000MS   ...

  4. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

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

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

  6. POJ 3928 Ping pong(树状数组+两次)

    题意:每个人都有一个独特的排名(数字大小)与独特的位置(从前往后一条线上),求满足排名在两者之间并且位置也在两者之间的三元组的个数 思路:单去枚举哪些数字在两者之间只能用O(n^3)时间太高,但是可以 ...

  7. POJ 3928 &amp; HDU 2492 Ping pong(树阵评价倒数)

    主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Descript ...

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

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

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

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

  10. UVALive-4329 Ping pong (树状数组)

    题目大意:有n个数排成一列,问从中能找出几个三元组(ai,aj,ak)满足i<j<k并且这三个数严格单调. 题目分析:枚举中间的数字aj,如果aj前面有c(j)个数a(j)小,后面有d(j ...

随机推荐

  1. Vue.js与 ASP.NET Core 服务端渲染功能整合

    http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...

  2. 消息队列MSMQ的使用

    1.MSMQ安装 控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定. 2.消息队列的应用场景(转载自http:/ ...

  3. React Native开发之expo中camera的基本使用

    之前做RN项目没调用过本地摄像头,今天下班早,做了一个简单的小demo:主要实现的功能:点击拍照按钮进入拍照界面,点击flip进行前后摄像头转换,点击开始拍照实现拍照功能(没写保存到本地的功能,大家可 ...

  4. Invoice Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messag ...

  5. django的验证码

    pip install Pillow==3.4.1在views.py中创建一个视图函数 from PIL import Image, ImageDraw, ImageFont from django. ...

  6. Docker入门系列01

    前两篇写了 Docker 如何安装和相关的概念,当然概念的东西省略了很多,主要是自己水平有限,所以后期会可能增添.但以上内容都是用别人的建好的 镜像(Image) ,这怎么行,我们应该自己动手造轮子, ...

  7. HTTPS相关知识以及在golang中的应用

    最近简单学习了HTTPS,并在golang中实践了一下,现在把学到的知识记录下来,方便以后查看,如果有幸能帮到有需要的人就更好了,如果有错误欢迎留言指出. 一些简单的概念,可以自行百度百科 HTTPS ...

  8. PS灰度蒙版建立

    通道里: 建立高光 1.Ctrl + 鼠标单击RGB通道-->得到高光 点击蒙版-->得到Alpha1(重命名高光通道): 2.然后紧接着Ctrl + shift + alt +鼠标单击A ...

  9. 理解C指针: 一个内存地址对应着一个值

    一个内存地址存着一个对应的值,这是比较容易理解的. 如果程序员必须清楚地知道某块内存存着什么内容和某个内容存在哪个内存地址里了,那他们的负担可想而知.    汇编语法对“一个内存地址存着一个对应的数” ...

  10. 北京Uber优步司机奖励政策(4月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...