这题不是一眼题,值得做。

思路:

  假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛。

  那么现在考虑如何求得和数组。根据的定义知道我们要求的就是区间中能力值小于第i个人的能力值的数量,即能力值在区间的人数。我们定义表示能力值为i的人的数量。利用树状数组从左到右动态更新与维护很容易得到数组,同理得到数组。

  注意:答案应该是longlong类型。


AC代码

//#define LOCAL
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
const int maxn = 20000+5;
int cnt[100000+5], Rank[maxn];
int left[maxn], right[maxn];
int maxRank;

int lowbit(int x) {
    return x&-x;
}

void init() {
    memset(cnt, 0, sizeof(cnt));
}

int getSum(int x) {
    int res = 0;
    while(x > 0) {
        res += cnt[x];
        x -= lowbit(x);
    }
    return res;
}

void update(int x, int d) {
    while(x <= maxRank) {
        cnt[x] += d;
        x += lowbit(x);
    }
}

void solve(int start, int end, int u, int *a) {
    for(int i = start; i != end; i += u) {
        a[i] = getSum(Rank[i]-1);
        update(Rank[i], 1);
    }
}

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL

    int T, n;
    scanf("%d", &T);
    while(T--) {
        init();
        maxRank = -inf;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%d", &Rank[i]);
            maxRank = max(maxRank, Rank[i]);
        }
        solve(0, n, 1, left);
        init();
        solve(n-1, -1, -1, right);
        LL ans = 0;
        for(int i = 0; i < n; i++) { //枚举每一位裁判
            ans += (LL)left[i] * (n-1-i-right[i]);
            ans += (LL)right[i] * (i-left[i]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

如有不当之处欢迎指出!

UVALive - 4329 Ping pong 树状数组的更多相关文章

  1. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  2. LA 4329 - Ping pong 树状数组(Fenwick树)

    先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...

  3. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  6. LA4329 Ping pong 树状数组

    题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...

  7. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  8. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  9. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

随机推荐

  1. JavaScript ECMAScript版本介绍

    1. 介绍 1.1 什么是ECMAScript ECMAScript,简称ES,是由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers ...

  2. localhost或本机ip无法连接数据库问题解决与原因

    解决办法:将localhost替换为127.0.0.1 原因@参考文章:navicat在电脑没有联网的情况下,并不会把localhost解析成127.0.0.1,而mysql默认情况下只支持127.0 ...

  3. chroot: failed to run command `/bin/bash': No such file or directory

    1 使用chroot命令时报错如下: testupgrade:/ # chroot /sb chroot: cannot change root directory to /sb: No such f ...

  4. jdk源码->集合->HashSet

    类的属性 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, ...

  5. struts 中自定义action访问方法

    struts中action类继承了ActionSupport  默认实现了execute()方法 struts.xml配置文件中 然后可以配置如下映射: <package name =" ...

  6. [DeeplearningAI笔记]改善深层神经网络_优化算法2.1_2.2_mini-batch梯度下降法

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.1 mini-batch gradient descent mini-batch梯度下降法 我们将训练数据组合到一个大的矩阵中 \(X=\b ...

  7. 使用命令行生成jar包

    测试用类 public class Hello { public static void main(String[] args) { System.out.println("hello wo ...

  8. 洛谷 [P3398] 仓鼠找sugar

    树剖求LCA 我们可以发现,两条路径ab,cd相交,当且仅当 dep[lca(a,b)]>=dep[lca(c,d)]&(lca(lca(a,b),c)==lca(a,b)||lca(l ...

  9. BZOJ 1076: [SCOI2008]奖励关 [DP 期望 状压]

    传送门 题意:$n$种宝物,出现$k$次每次一种,每种宝物有价值和吃掉它之前必须要吃掉的宝物的集合,求采取最优策略的期望最大价值 1<=k<=100,1<=n<=15,分值为[ ...

  10. BZOJ 2142: 礼物 [Lucas定理]

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1294  Solved: 534[Submit][Status][Discuss] ...