HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)
Description
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
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
题目大意:有一个互不相等的序列,问有多少个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)的更多相关文章
- HDU 2492 Ping pong (数状数组)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Ping pong(树状数组求序列中比某个位置上的数小的数字个数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
- POJ 3928 Ping pong(树状数组)
Ping pong Time Limit: 1000MS ...
- LA4329 Ping pong(树状数组与组合原理)
N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...
- UVALive 4329 Ping pong(树状数组)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...
- POJ 3928 Ping pong(树状数组+两次)
题意:每个人都有一个独特的排名(数字大小)与独特的位置(从前往后一条线上),求满足排名在两者之间并且位置也在两者之间的三元组的个数 思路:单去枚举哪些数字在两者之间只能用O(n^3)时间太高,但是可以 ...
- POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)
主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Descript ...
- LA 4329 Ping pong (树状数组)
题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
- ACM-ICPC LA 4329 Ping pong(树状数组)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive-4329 Ping pong (树状数组)
题目大意:有n个数排成一列,问从中能找出几个三元组(ai,aj,ak)满足i<j<k并且这三个数严格单调. 题目分析:枚举中间的数字aj,如果aj前面有c(j)个数a(j)小,后面有d(j ...
随机推荐
- iOS 文件下载及断点续传
ios的下载我们可以使用的方法有:NSData.NSURLConnection.NSURLSession还有第三方框架AFNetworking和ASI 利用NSData方法和NSURLConnecti ...
- 通过ES6写法去对Redux部分源码解读
在Redux源码中主要有四个文件createStore,applyMiddleware,bindActionCreators,combineRedures createStore.js export ...
- 复习宝典之Maven项目管理
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第二章:Maven项目管理 Maven是基于项目对象模型(POM project object model),可以通过一小段描述信息(配 ...
- 08JavaScript对象
JavaScript 对象是拥有属性和方法的数据. 注:在 JavaScript 中,对象是非常重要的,当你理解了对象,就可以了解 JavaScript . 1.JavaScript 对象 在 Jav ...
- UIPickerView的简单使用
UIPickerView是一个选择器它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,使用也比较简单.下面做了一个关于天气预报的小Demo 用 UI ...
- 课时133.margintop失效原因(理解)
我们之前讲过如果只有子元素设置了margin top而父元素没有边框则会跟着被顶下来的. 而我们怎么解决这个问题呢? 就是给父元素设置一个边框 而为什么我们在第二个浮动的盒子设置边框没有用呢?应为第一 ...
- C/C++中的malloc、calloc和realloc
1. malloc 原型:extern void *malloc(unsigned int num_bytes); 头文件:Visual C++6.0中可以用malloc.h或者stdlib.h 功能 ...
- Waltz of love
Waltz of love Love me tenderly Love me softly Close your eyes,fling to the dangcing hall Follow your ...
- python3 练习题100例 (一)
断断续续的学了很久的python,有很多又忘记了.从今天开始用实例再进行一次学习,并记录.本人小白一个,请大家多多指教. #!/usr/bin/env python3 # -*- coding: ut ...
- git小技巧之分支、关联远程仓库、回滚、解决.gitignore不生效等
1.分支管理 新建并切换分支:git checkout -b <name>新建本地分支并关联到远程分支git checkout -b myRelease origin/Release合并某 ...