zoukankan      html  css  js  c++  java
  • 高级数据结构第六章A . 逆序对

    image

    代码:

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    
    
    using namespace std;
    typedef long long LL;
    
    const int N = 1e5+10;
    
    LL tr[N],n,a[N];
    vector<LL>nums;
    
    LL lowbit(int x)
    {
        return x & -x;
    }
    
    void update(int x, int c)  
    {
        for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
    }
    LL query(int x)  
    {
        int res = 0;
        for (int i = x; i; i -= lowbit(i)) res += tr[i];
        return res;
    }
    
    
    int main()
    {
        cin>>n;    
        for (int i = 1; i <= n; i ++ ) cin>>a[i],nums.push_back(a[i]);
        sort(nums.begin(),nums.end());
        nums.erase(unique(nums.begin(),nums.end()),nums.end());
        int m=nums.size();
       // cout<<m<<endl;
        LL res=0;
        for (int i = 1; i <= n; i ++ ){
            int tmp=lower_bound(nums.begin(),nums.end(),a[i])-nums.begin()+1;
            res+=query(m)-query(tmp);
            update(tmp,1);
        }
        cout<<res<<endl;
        
        return 0;
    }
    
    
  • 相关阅读:
    面试问题
    知识点整合
    前端错误
    基于.NET平台常用的框架整理
    BFC和haslayout
    javascript面向对象
    javascript变量的作用域
    2014-05-26 总结
    2014-05-23 总结
    PHP实现mvc模式的思想
  • 原文地址:https://www.cnblogs.com/OvOq/p/14881151.html
Copyright © 2011-2022 走看看