chenfy27的刷题记录

导航

leetcode493 统计重要翻转对的数目

给定一个数组nums[n],如果i<j并且nums[i]>2*nums[j],则称(i,j)是一个重要翻转对。求nums[n]中重要翻转对的数量。
1<=n<=5e4; nums[i]在int范围内

分析:直接套平衡树模板即可。

// Treap模板...

class Solution {
public:
    int reversePairs(vector<int>& nums) {
        Treap<long long> tp;
        int ans = 0;
        for (auto i : nums) {
            ans += tp.gtcnt(2LL * i);
            tp.insert(i);
        }
        return ans;
    }
};

posted on 2024-03-16 10:27  chenfy27  阅读(11)  评论(0)    收藏  举报