题解 | #数组中的逆序对#

数组中的逆序对

http://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5

const int maxn=1e5+5;
const int mod=1e9+7;
class Solution {
public:

    #define lowbit(x) x&(-x)

    int sum[100005];
    vector<int> p;
    void add( int x ) { while(x<maxn ){ sum[x]++,x+=lowbit(x);} };
    int query( int x ,int ans=0 ) {while(x)ans=(ans+sum[x])%mod,x-=lowbit(x);return ans; } 
    int getId( int x ) { return lower_bound(p.begin(),p.end(),x)-p.begin()+1; }
    int InversePairs(vector<int> data) {
        p.clear();
        for( int v:data ) p.push_back(1e5-v);
        sort(p.begin(),p.end());
        p.erase(unique(p.begin(),p.end()),p.end());

        int ans=0;
        for( int v:data ){
            v=1e5-v;
            v=getId(v);
            add(v);
            ans=(ans+query(v-1))%mod;
        }
        return ans;
    }
};
全部评论

相关推荐

01-17 12:35
吉首大学 Java
秋招之BrianGriffin:自己的工作自己做!😡
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务