Ping pong

                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)  

链接:hdu 2492

Problem 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.
Sample Input
  
1 3 1 2 3
Sample Output
  
1
Source
2008 Asia Regional Beijing

题意

有N个人从左到右排成一排,每个人有一个唯一的技能值,选出三个人——两名比赛选手还有一名裁判,裁判的技能值不能比这两个人都高,也不能比这两个人都低,并且这两个人到裁判的距离总和不能大于他们之间的距离。不同的人比赛或者比赛时候的裁判不同算不同的比赛,求一共能比几场。

分析

将题意抽象出来,就是已知数列{aN}(3<=N<=20000)其中ai,从左往右取三个不同的数(可以不相邻),求使这三个数排成升序或降序的取法数。

设L为第i个人左边的人中,技能值小于他的人数, R为第i个人左边的人中,技能值小于他的人数,那么选第i个人作为裁判的方法数Ans[i]等于(L[i] * (N - i - R[i]) +(i - 1 - L[i]) * R[i]),最终输出的答案就是枚举N个人,对Ans[i]求和。那么现在的问题就是如何求L和R数组,那么很清晰,直接树状数组对区间求和即可,方法类似于求逆序数。

/****************************>>>>HEADFILES<<<<****************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
#define fst             first
#define snd             second
#define root            1,N,1
#define lson            l,mid,rt<<1
#define rson            mid+1,r,rt<<1|1
#define PB(a)           push_back(a)
#define MP(a,b)         make_pair(a,b)
#define CASE(T)         for(scanf("%d",&T);T--;)
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//typedef __int64         LL;
typedef long long       LL;
typedef pair<int, int>   PII;
const int INF = 0x3f3f3f3f;
const int maxn = 100000 + 5;
const int maxm = 20000 + 5;
/****************************>>>>SEPARATOR<<<<****************************/
int T, N, MAX, a[maxm];
int S[maxn], L[maxm], R[maxm];
inline int lowbit(int x) { return x & (-x); }
void Add(int pos, int val)
{for(; pos <= MAX; pos += lowbit(pos)) S[pos] += val;
}
int Query(int pos)
{int ret = 0;for(; pos > 0; pos -= lowbit(pos)) ret += S[pos];return ret;
}
int main()
{
//    FIN;CASE(T){scanf("%d", &N);memset(S, 0, sizeof(S));MAX = 0;for(int i = 1; i <= N; i++){scanf("%d", &a[i]);MAX = max(MAX, a[i]);}for(int i = 1; i <= N; i++){L[i] = Query(a[i]);Add(a[i], 1);}memset(S, 0, sizeof(S));for(int i = N; i >= 1; i--){R[i] = Query(a[i]);Add(a[i], 1);}LL ans = 0;for(int i = 2; i < N; i++){ans += (L[i] * (N - i - R[i]) + (i - 1 - L[i]) * R[i]);}cout << ans << endl;}
}

POJ 3928 hdu 2492 Uva1428 PingPong 【树状数组】相关推荐

  1. HDU ACM 4031 Attack (树状数组--单点查询+区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 用了树状数组的区间更新 单点查找(一般为单点更新 区间查找) 例如 区间(2,4)加1 则Updata(2 ...

  2. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

  3. hdu 4125 Moles(kmp+树状数组)

    题目链接:hdu 4125 Moles 题意: 给你n个数,让你按键值建一个平衡二叉树,然后奇数为0,偶数为1,然后可以遍历这颗树得到一个欧拉序列,现在给你一个串,问你出现了几次. 题解: 建树的时候 ...

  4. hdu 1892二维树状数组

    这题我知道是用树状数组,可是好久没打树状数组了,就想用普通方法水过去~~结果--结果--水了好多方法都水不过,出题人真狠呐--我的水方法是对于每一次查询,初始化ans=(x2-x1+1)*(y2-y1 ...

  5. hdu 4417 Super Mario 树状数组||主席树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  6. HDU 1556 前缀和 树状数组 线段树

    解法一: a[i]表示以 i作为起点,对 i-n的气球全部上色的次数  对(start,end)区间上色 ++a[start] --a[end+1]抵消掉 end+1-n的部分 问题转换为求 a的前缀 ...

  7. HDU - 5775 Bubble Sort(树状数组)

    题目链接:点击查看 题目大意:给出n个数,求出按照冒泡排序期间每个数可以到达的最右边位置和最左边位置的差 题目分析:其实这个题想明白了就很简单了,先用辅助数组a按照顺序存储每一个数,a[i]就代表排序 ...

  8. HDU 4777 Rabbit Kingdom 树状数组

    分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...

  9. hdu 4267 多维树状数组

    题意:有一个序列 "1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i ...

最新文章

  1. 公司这套架构统一处理 try...catch 这么香,求求你不要再满屏写了,再发现扣绩效!...
  2. Oracle数据库用户角色、表空间创建、删除命令
  3. 蜗轮蜗杆计算软件_齿轮传动计算软件
  4. 15.立体几何——立体,基本概念,随机点立体图,立体估计深度_2
  5. 当上项目经理才知道!linuxmysql执行sql文件命令
  6. 八月为什么有31天?
  7. error:unable to recogni “*.yaml“ze no matcher for kind “Deployment“ in version “extensions/b1beta1“
  8. js生成web安全色
  9. 如何让程序运行后不谈程序兼容性助手
  10. 用 .NET 3.5 创建 ToJSON() 扩展方法 (木野狐译)
  11. 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)
  12. word2vec-0.11.1安装包以及可能遇见的问题解决方法
  13. 60种数据可视化图表总结(文末送书)
  14. 虎年啦,小老虎5分钟带你学会Linux中的移动-改名(mv)
  15. 华为鸿蒙系统开发语言,华为鸿蒙系统采用什么语言进行开发的
  16. Problem B – Buggy ICPC——思维,找规律
  17. java毕业生设计校园兼职招聘系统计算机源码+系统+mysql+调试部署+lw
  18. 维吉尼亚密码原理图解
  19. Win32学习笔记 第三章 HelloWin 选择自 villager 的 Blog
  20. Beyond MapReduce:谈2011年风靡的数据流计算系统

热门文章

  1. latex公式大括号的使用方法
  2. 简单谈谈parseInt
  3. 愤怒的TryCatch
  4. 数据分析初探——以2020百度西安交大大数据竞赛:传染病感染人数预测为例
  5. 数据结构复习笔记(基本概念)
  6. sierpinski三角形的维数_谢尔宾斯基(Sierpinski)三角形
  7. 构造一个Car类,包含无参和有参两种构造方法
  8. 项目选题报告(待就业六人组)
  9. 全志H3、H8方案机顶盒通刷教程
  10. 【好文翻译】Difference Between Next.js vs. Nuxt.js vs. Nest.js