UVALive 4329 Ping pong(树状数组)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895
题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在每场比赛需要3个人,两个选手,一个裁判;裁判须住在他们之间,且其技能值必须在两选手之间,求一共能组织多少种比赛。
注意到技能值的范围(1 ≤ ai ≤ 100000),所以我们可以树状数组(O(nlogn))处理得到一个ll数组,ll[x]表示住在x左边的并且技能值小于x的技能值的人数。类似逆着处理一次,得到一个rr数组,rr[x]表示住在x右边的并且技能值小于x的技能值的人数。
最后的答案便是:
ans += ll[i] * (n - i - rr[i]) + (i - 1 - ll[i]) * rr[i] (1<= x <= n)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
#define maxm 20010
using namespace std; typedef long long LL; int player[maxm], c[maxn], ll[maxn], rr[maxn], n; int lowbit(int x); void add(int x, int u); int sum(int x); int main(void)
{
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &n);
for (int i = ; i <= n; ++i)
{
scanf("%d", player + i);
}
memset( c, , sizeof(c));
for (int i = ; i <= n; ++i)
{
ll[i] = sum( player[i] - );
add( player[i], );
}
memset( c, , sizeof(c));
for (int i = n; i >= ; --i)
{
rr[i] = sum( player[i] - );
add( player[i], );
}
LL ans = ;
for (int i = ; i <= n; ++i)
{
ans += ll[i] * (n - i - rr[i]) + (i - - ll[i]) * rr[i];
}
printf("%lld\n", ans);
}
return ;
} int lowbit(int x)
{
return x&(-x);
} void add(int x, int u)
{
while (x <= maxn)
{
c[x] += u;
x += lowbit(x);
}
} int sum(int x)
{
int result = ;
while (x > )
{
result += c[x];
x -= lowbit(x);
}
return result;
}
UVALive 4329 Ping pong(树状数组)的更多相关文章
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
随机推荐
- nullcom HackIM2016 -- Programming Question 4
One of the NullCon vidoes talked about a marvalous Russian Gift. The Vidoe was uploaded on [May of 2 ...
- (转)提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- 向 Git 服务器添加 SSH 公钥
. . . . . 在网上很少找到文章有提到如何将自己的 Git 远程仓库配置成可以通过 SSH 公钥方式认证的,而几乎清一色都是告诉你怎么通过 web 界面向 GitHub 添加 SSH 公钥.LZ ...
- aspx页面,中文乱码解决方案
由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...
- grunt 基本使用使用(一)。
使用grunt 之前,需要做一些基本工作. 1.在E盘 新建空文件夹 grunt. 2.在grunt目录下新建package.json 文件,用了存储 npm模块的依赖项.基本依赖块代码如下: { & ...
- JS产生随机一注彩票
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- java 汉语转拼音(全拼,首字母)
import java.util.*; import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.for ...
- 未添加document.ready产生的BUG
今天在框架里使用superslide插件时,在javascript部分 <script type="text/javascript"> jQuery(".yj ...
- Android Studio 简介及导入 jar 包和第三方开源库方[转]
原文:http://blog.sina.com.cn/s/blog_693301190102v6au.html Android Studio 简介 几天前的晚上突然又想使用 Android Studi ...
- With语句以及@contextmanager的语法解析
with 语句以及@contextmanager的语法解析 with语句可以通过很简单的方式来替try/finally语句. with语句中EXPR部分必须是一个包含__enter__()和__e ...