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. [题 ...
随机推荐
- 串口接收模块(verilog) 波特率115200
我来分享一下uart协议之接收verilog代码 顶层实例化 `timecale 1ns / 1ps////////////////////////////////////////////////// ...
- 算法:javascript截取字符串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- ASP.NET Core中的OWASP Top 10 十大风险-失效的访问控制与Session管理
不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: https://dotnetcoretutorials.com/201 ...
- day2--第2章(计算机系统硬件核心知识)
第二章--计算机系统核心硬件知识 (一)互联网企业里PC服务器品牌及型号 互联网公司服务器品牌: DELL(大多数公司),HP,IBM(百度),浪潮,联想,航天联志. Dell服务器品牌: 1U = ...
- 清理win10过期补丁的命令
作用是删除已经被新版本取代的旧系统文件 DISM.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase 注1: 执行后, 补丁就无法 ...
- 我是如何理解Android的Handler模型_2
对比例程说明,如: 例:在新新线程中替换TextView显示内容. 界面如下,单击按键后original data 替换为 changed data Handler Message部分实现步骤: 1. ...
- .Net主线程扑捉子线程中的异常
首先看一段C#代码:运行后发现主线程通过try{}catch{}是不能扑捉子线程中的抛出来的异常. 代码 ); } public void run() { ...
- Linux端图形处理工具ImageMagick在Centos上的安装
一.安装背景程序要用到用户上传图片,编辑的功能,能进行旋转,裁剪,缩放等. 二.ImageMagick介绍 ImageMagick是用C语言开发图片处理程序.可以对图片进行改变大小.旋转.锐化.减色或 ...
- Drools文档(六) 用户手册
用户手册 基础 无状态的知识Session Drools规则引擎拥有大量的用例和功能,我们要如何开始?你无须担心,这些复杂性是分层的,你可以用简单的用例来逐步入门. 无状态Session,无须使用推理 ...
- CKEdit( htm编辑器)
http://ckeditor.com/ (强大的在线编辑器)