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. [题 ...
随机推荐
- jQuery选择器(可见性选择器)第五节
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Problem B: 大整数的加法运算 升级版
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
- asp.net core 开发的https证书服务-agilelabs.net
创建证书-生成CSR(Certificate Sign Request): 填写证书基本信息 接下来我们就可以看到创建的证书签名请求信息(CSR): 为我们刚才创建的CSR签名: 签名的意思是说通过证 ...
- 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan(四)
功能说明 该步骤实现的功能包括: 1. 启动程序时,将@ComponentScan加载的类,创建对象并放在容器里面. 2. 通过ApplicatoinContext的getBean()方法获得容器里面 ...
- 全排列Permutations
描述 Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the foll ...
- C#的Random到底该怎么使用
先看代码: 在循环中,有的只NEW一个Random,有的每次都NEW 一个Random. Console.WriteLine("1.多个Random,默认随机种子,"); ; i ...
- .net表达式计算器(中缀表达式转后缀表达式,支持20多个数学函数,支持函数嵌套)
最近在网上查了一下表达工计算器的类库,发现Java版本的有一个比较成熟的叫W3EVal,好像是一个IBM工程师写的,.net就很少了(可能是我了解不够多),但投机取巧的实现思路有很多,比如: (1)将 ...
- numpy初识
1,机器学习numpy 初识 1)numpy初识 import numpy num1= numpy.array([1,2,3]) dtype('num1') #查找类型 num1.dtype num1 ...
- 实现基于tomcat集群会话保持
1.实验环境 我们需要准备两台虚拟机,把这两台虚拟机组成集群,实现会话保持. 2.配置server1 2.1 修改nginx配置文件 [root@server1 ~]# vim /etc/nginx/ ...
- java虚拟机概述
java 虚拟机是什么? java虚拟机是一个将字节码指令映射为对应物理操作系统指令的程序. java程序的运行需要事先安装 jdk,而在jdk内部的jre中其核心就是 jvm ...