UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong
题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人。而且技能值从小到大或从大到小,问有几种举办形式
思路:利用树状数组处理出每一个位置左边比它小的个数和右边比他小的个数和。那么左边和右边大就也能计算出来,那么比赛场次为左边小*右边大+左边大*右边小。
代码:
#include <cstdio>
#include <cstring> const int N = 100005; int t, n, a[N];
long long bit[N], lx[N], rx[N]; inline int lowbit(int x) {
return x&(-x);
} void add(int x, long long val) {
while (x < N) {
bit[x] += val;
x += lowbit(x);
}
} long long Query(int x) {
long long ans = 0;
while (x > 0) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
memset(bit, 0, sizeof(bit));
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
lx[i] = Query(a[i]);
add(a[i], 1);
}
memset(bit, 0, sizeof(bit));
for (int i = n - 1; i >= 0; i--) {
rx[i] = Query(a[i]);
add(a[i], 1);
}
long long ans = 0;
for (int i = 0; i < n; i++)
ans += lx[i] * (n - i - 1 - rx[i]) + (i - lx[i]) * rx[i];
printf("%lld\n", ans);
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
UVA 1428 - Ping pong(树状数组)的更多相关文章
- 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 ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- 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 ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
随机推荐
- Anaconda的安装
Windows下Anaconda的安装和简单使用 Anaconda is a completely free Python distribution (including for commercial ...
- AMP Physical Link Creation And Disconnect
A flow diagram of the AMP link establishment and detachment of a connection between two devices is s ...
- [Angular] Learn Angular Multi-Slot Content Projection
Now for au-modal component, we pass in tow component though contenct projection: <au-modal class= ...
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- [Angular] Creating an Observable Store with Rx
The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...
- strace跟踪线程调用
方法一:strace -fp pid , 可以跟踪所有线程, 进程的系统调用. [root@xxxx]strace -p 24091 Process xxx attached - interrupt ...
- [Elm] Functions in Elm
Functions are an important building block in Elm. In this lesson we will review stateless functions, ...
- [Python学习] 简单爬取CSDN下载资源信息
这是一篇Python爬取CSDN下载资源信息的样例,主要是通过urllib2获取CSDN某个人全部资源的资源URL.资源名称.下载次数.分数等信息.写这篇文章的原因是我想获取自己的资源全部的评论信息. ...
- 使用truss、strace或ltrace诊断软件的"疑难杂症"
原文链接 简介 进程无法启动,软件运行速度突然变慢,程序的"Segment Fault"等等都是让每个Unix系统用户头痛的问题,本文通过三个实际案例演示如何使用truss.str ...
- thinkphp,onethink,thinkox验证码不显示
使用验证码的时候,一开始正常,后来不显示了 网上说是utf-8的编码问题,什么bom去掉,转化为无bom的格式 我都试了,没用 后来知道是在调用验证码的地方 写上 Public function v ...