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. ZXing.net 生成和解析二维码

    nuget引用zxing.net包 public partial class Form1 : Form { public Form1() { InitializeComponent(); } priv ...

  2. 协作开发中常用的Git命令小结

    先提一下最基础的git命令用法: git clone   从远端克隆到本地仓库 git add . (注意add和. 之间有一个空格)将全部改动添加到暂存区 git checkout xxx 撤销更改 ...

  3. 『ACM C++』 PTA 天梯赛练习集L1 | 046-47

    今日刷题 ------------------------------------------------L1-046----------------------------------------- ...

  4. centos7 php7 动态编译mysqlnd: configure: error: Cannot find OpenSSL's <evp.h> 错误解决

    开始以为是没有安装openssl, openssl-devel,安装后发现还是提示这个错误,搜索了一下evp.h,这个文件也存在.GOOGLE 了一下,在stackoverflow,找到了答案,原来是 ...

  5. Java5~11新特性

    Java5~11版本新特性 Java5 Java6 Java7 Java8 Java9 Java10 Java11 Java5 Java5开发代号为Tiger(老虎),于2004-09-30发行 特性 ...

  6. percona数据库监控工具的安装部署

    Percona Monitoring and Management 安装 PMM是一个开源,免费的mysql管理监控平台,他可以用来分析mysql,mariadb和mongodb的服务器性能. 安装步 ...

  7. IIS网站的应用程序与虚拟目录的区别及应用

    IIS网站 一个网站可以新建无数个应用程序和目录 应用程序 同一域名下程序的独立开发,独立部署的最佳应用策略. 应用程序的应用场景: 1. 域名的分布 比如:www.baidu.com,对于后台,我们 ...

  8. jQuery.qrcode 生成二维码,并使用 jszip、FileSaver 下载 zip 压缩包至本地。

    生成二维码 引用 jquery.qrcode.js  :连接:https://files.cnblogs.com/files/kitty-blog/jquery.qrcode.js .https:// ...

  9. 纯js轮播图练习-1

    偶尔练习,看视频自己学着做个简单的纯JS轮播. 简单的纯js轮播图练习-1. 样子就是上面图片那样,先不管好不好看,主要是学会运用和理解轮播的原理 掌握核心的理论知识和技术的操作,其他的都可以在这个基 ...

  10. C语言实验报告(五) 两个正整数的最大公约数

    编程实现求两个正整数的最大公约数,要求计算最大公约数用函数fun(int a,int b)实现. #include<stdio.h>void main(){  int n,a,b;  in ...