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 ...
随机推荐
- nopCommerce电子商务平台 安装教程(图文)
nopCommerce是一个通用的电子商务平台,适合每个商家的需要:它强大的企业和小型企业网站遍布世界各地的公司销售实体和数字商品.nopCommerce是一个透明且结构良好的解决方案,它结合了开源和 ...
- linux 第十天学习
一.RAID 1.常见RAID (RAID 0.RAID1.RAID5.RAID10) 2.RAID 10 阵列添加 2.1.添加硬盘 2.2.查看系统加载 2.3.mdadm 命令添加RAID阵列 ...
- Sass变量及嵌套
1. 变量:SASS允许使用变量,所有变量以$开头. 变量声明:$highlight-color: #000; 注意:变量可以在css规则块定义之外存在.如下例子: $nav-color: #F90; ...
- 常用贴片三极管型号与丝印的对应关系(SOT23)
个人常用贴片三极管型号与丝印的对应关系(SOT23): 丝印:Y1 型号:8050,NPN型三极管 丝印:Y2 型号:8550,PNP型三极管 丝印:L6 ...
- python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy
windows下安装scrapy 依次执行下列操作: pip install wheel pip install lxml pip install PyOpenssl 安装Microsoft visu ...
- Python写网络后台脚本
Python写网络后台脚本. 首先安装Python3.6.5,在centos中自带的Python是2.6版本的,现在早就出现了3.6版本了况且2和3 之间的差距还是比较大的,所以我选择更新一下Pyth ...
- vue中cssModules理解可以用于加密和避免重复使用
cssModules可以用于加密和避免重复使用,也就是说可以在当前vue文件中写的样式会生成独一无二的名字,在其他vue文件中是无法调用的, 一.可以直接配cssModules 第一步,配置vue-l ...
- 算法-PHP实现八大算法
八大算法原理详解 交换函数:注意要按引用传递,否则无法真正交换两个数的值 function exchange(&$a, &$b){ $temp = $a; $a = $b; $b = ...
- 广州Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Java语言简介
Java即计算机编程语言 1.概念 Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Jav ...