zoukankan      html  css  js  c++  java
  • b_lc_通过指令创建有序数组(树状数组+细节)

    根据数组instructions中的元素创建一个有序数组nums,每一次插入操作的 代价 是以下两者的 较小值(n<=1e5):

    • nums中 严格小于 instructions[i] 的数字数目。
    • nums中 严格大于 instructions[i] 的数字数目。

    思路:用树状数组维护a[i]维护≤i的元素的个数,lo很好求 ask(A[i]-1),hi的两种求法:

    1. ask(max(A))-ask(A[i])
    2. dup数组记录重复的元素的个数,hi=i-lo-dup[A[i]
    const int N=1e5+5, mod=1e9+7;
    class Solution {
    public:
        int n,ans,a[N],dup[N];
        int lowbit(int x) {return x&-x;}
        void add(int x, int v) {
            for (; x<N; x+=lowbit(x)) a[x]+=v;
        }
        int ask(int x) {
            int c=0;
            for (; x; x-=lowbit(x)) c+=a[x], c%=mod;
            return c;
        }
        int createSortedArray(vector<int>& A) {
            n=A.size();
            for (int i=0; i<n; i++) {
                int lo=ask(A[i]-1), hi=ask(N-2)-ask(A[i]); //小于等于A[i]-1的数的个数
                ans=(ans+min(lo, hi))%mod; 
                add(A[i],1);
            }
            return ans;
        }
    };
    
  • 相关阅读:
    树分治 poj 1741
    堆 poj 2010
    堆 poj 2442
    堆的基本操作
    状态压缩codeforces 11 D
    状态压缩 CSU1129 送货到家
    炮兵阵地 POJ 1185
    状态压缩 HDU4539 郑厂长系列故事——排兵布阵
    状态压缩 HDU 3182
    android手势创建及识别
  • 原文地址:https://www.cnblogs.com/wdt1/p/13944505.html
Copyright © 2011-2022 走看看