HackerRank - "Triplets"

Good one to learn Binary Indexed Tree (Fenwick Tree). Simply grabbed the editorial‘s code but with comments.

#include <cmath>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define MAX_NUM 100001

/////////////////////////
class FenwickTree
{
    long long tree[MAX_NUM];
public:
    //    Utility methods: in terms of the original array BIT operates on
    //
    FenwickTree()
    {
        for(int i = 0; i < MAX_NUM; i ++)
            tree[i] = 0;
    }
    long long get(int inx)
    {
        return query(inx) - query(inx - 1);
    }
    void set(int inx, long long val)
    {
        long long curr = get(inx);
        update(inx, val - curr);
    }
    //    BIT core methods
    //
    void update(int inx, long long val)
    {
        //    BIT: from child going up to tree root
        //         so ?????????? last valid bit in each iteration
        //
        while(inx <= MAX_NUM)
        {
            tree[inx] += val;
            inx += (inx & -inx);    // backwards as query
        }
    }
    long long query(int inx)
    {
        long long sum = 0;
        //    BIT: from parent going down to children
        //         so eliminating last valid bit in each iteration
        //         like addition in binary representation
        while(inx > 0)    // lower valid bits to higher valid ones. cannot be zero
        {
            sum += tree[inx];
            inx -= (inx & -inx);
        }
        return sum;
    }
};

FenwickTree t1, t2, t3;

/////////////////////////

int main()
{
    set<int> s;
    map<int, int> m;

    //    Get input
    int n; cin >> n;
    vector<int> in(n);
    for(int i = 0; i < n; i ++)
    {
        cin >> in[i];
        s.insert(in[i]);
    }

    //
    int cnt = 1;
    for (auto &v : s)    // sorted
        m[v] = cnt ++;

    for(auto &v : in)
    {
        int i = m[v];
        t1.set(i, 1);    // Existence: there‘s one number at i-th position in a sorted sequence
                        //              and then t1 updates all accumulated records (+1 all the way up)
        t2.set(i, t1.query(i - 1)); // set number of sum(existed smaller numbers): no. of tuples
        t3.set(i, t2.query(i - 1)); // similar as above
    }
    cout << t3.query(MAX_NUM) << endl;
    return 0;
}
时间: 2024-08-07 08:27:45

HackerRank - "Triplets"的相关文章

Bonetrousle HackerRank 数学 + 思维题

https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n. 首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi.最大值,后b个数相加,记为mx 注意到一个东西:如果mi <= n <= mx.那么是绝对可行的.因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解. 所以首先从mi开始枚举,

hackerrank maxsum mod

https://www.hackerrank.com/challenges/maximise-sum/submissions/code/12028158 hackerrank 子数组和模m的最大值 时间复杂度nlgn, 主要是证明一点,presum[i]-presum[j] 对于0<j<i的j,最大的是第一个大于presum[i]的presum[j] 证明如下 Quro的讨论,感觉还是证明的过程不够细致 http://www.quora.com/What-is-the-logic-used-i

房价预测(HackerRank)

从今天开始要多做一些关于机器学习方面的竞赛题目,题目来源主要是Hackerrank和Kaggle.链接如下 Hackerrank:https://www.hackerrank.com/ Kaggle:https://www.kaggle.com/ 在Hackerrank中提交源代码,这就使得很多库都需要自己写,限制比较多.而Kaggle只需要提交数据,所以随便怎么搞都行.现在来讲第一道题,房价预测,这是Andrew Ng课程里的比较经典的例子.题目描述如下 题目:https://www.hack

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

【HackerRank】Bus Station

有n组好朋友在公交车站前排队.第i组有ai个人.还有一辆公交车在路线上行驶.公交车的容量大小为x,即它可以同时运载x个人. 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车. 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车.另外,每个人不想失去自己的位置,即组的顺序不会改变. 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)? 输入格式 第一行只包含一个整数n.第二行包含n个空格分

日常小测:颜色 &amp;&amp; Hackerrank Unique_colors

题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献是独立的.我们可以在一次点分治中合在一块处理(为什么时间复杂度是对的呢,因为我们每次改动只会根据当前点的颜色进行变动,而不是所有颜色对着它都来一遍).每次先对重心单独计算答案贡献,此时也将当前区域的各个答案贡献计算出来,并以此为基础(之后称之为基准贡献,即代码中的tot).对于每一棵子树,我们先df

hackerrank:Almost sorted interval

题目链接:https://www.hackerrank.com/challenges/almost-sorted-interval 题目大意: 定义一个“几乎单调”区间(区间最小值在最左面,最大值在最右面) 给一个N的排列,求“几乎单调”区间的个数 N=100W  解法为O(n) 很好的思维题! 想了一下午,其实自己离正解已经不远了,,不过最后还是看了学长的ac代码 然后基本上秒懂了 具体思维方式不好说啊..贴个代码,以后又不会了的话慢慢回忆吧= =|| #include <iostream>

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For