POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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?
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).
1
3 1 2 3
1
题意
有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 【树状数组】的更多相关文章
- POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)
主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Descript ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 4000 Fruit Ninja (树状数组+反向思维)
题意:给你一串数且每个数都不同,问你(x,y,z)出现 x<z<y 的总次数 首先我们直接想的话不能使用O(n*log2 n)解决,所以可以正难则反 可以求得x<(y,z)的值,减去 ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
随机推荐
- asp.net 自定义的模板方法接口通用类型
本来想写这个帖子已经很久了,但是公司事情多,做着做着就忘记了.公司因为需要做接口,而且用的还是asp.net的老框架,使用Handler来做,没得办法,自己照着MVC写了一个通过的接口操作模板. 上送 ...
- 《剑指offer》面试题的Python实现
<剑指Offer>是很多程序员面试前要看的书,但里面的算法都是基于C++实现的,最近用了三周左右时间,用Python完成了里面几乎所有的算法题,由于时间以及个人水平均有限,或许会有部分问题 ...
- Magicodes.Admin.Core开源框架总体介绍
框架说明 Magicodes.Admin.Core框架在ABP以及ASP.NET ZERO的基础上进行了封装和完善,目前基于.NET Core 2.0+(Framework版本),由于部分组件在.NE ...
- 基于IWICImage的截图代码
截图方式和以前一样, 用GetDC, 保存为JPG的方式改用IWICImage接口, 在我机器上 1920*1080 大概花费70毫秒左右, 比用TJPEGImage快了一倍多(TJPEGImage需 ...
- 开篇-我眼中的FPGA
既然是开篇,那就来闲话叨一叨FPGA吧. 掰掰指头,结识FPGA估计有5年多.作为嵌入式工程师,每天的日常充斥着ARM.DSP.操作系统.通讯.总线等耳熟能详的词汇,徜徉其中不能自拔,而这其中,自觉最 ...
- Oracle常用的数值函数,日期函数
---恢复内容开始--- 数值函数 常用的处理数值的函数有如下: No. 函数名 含义 1 round(x[,y]) 返回四舍五入后的值 2 trunc(x[,y]) 不会四舍五入 3 mod(x,y ...
- 全内存的redis用习惯了?使用基于硬盘存储类似redis的nosql产品ssdb呢?
首先说一下背景,在双十一的时候,我们系统接受X宝的订单推送,同事原先的实现方式是使用redis的List作为推送数据的承载,在非大促的场景下, 一切运行正常,内存占用大概3-4G,机器是16G内存.由 ...
- VMWare安装Win10虚拟机
这两天突发奇想安了个win10虚拟机,在安装的过程中还遇到了不少麻烦,所以在此与大家分享下. 首先我们用VMWare12来安装,VMWare已经更新到14但是不太稳定,所以为了保险起见还是用12吧. ...
- BST 解析 (二)height and deletion
前面一章介绍了BST的结构和一些简单的基本功能,例如:insert,findMin,nextLarger等等.这一节主要讲解一些BST的delete node操作还有BST的height的分析以及一些 ...
- Java中的回调
又忙了一周,事情差不多解决了,终于有可以继续写我的博客了(各位看官久等了). 这次我们来谈一谈Java里的一个很有意思的东西--回调. 什么叫回调,一本正经的来讲,在计算机程序设计中,回调函数是指通过 ...