博客
关于我
谜一样的牛
阅读量:230 次
发布时间:2019-02-28

本文共 1480 字,大约阅读时间需要 4 分钟。

?n????????????1?n?????????????????????????????????

????

?????????????????????????????????????Fenwick Tree??????????????????????????

???????

  • ???????????????1????????????
  • ?????????????????????
  • ?????????????????????????????
  • ?????????????????????????????????
  • ????

    #include 
    #include
    #include
    using namespace std;int lowbit(int x) { return x & -x;}void add(int x, int c, int n, vector
    & tr) { while (x <= n) { tr[x] += c; x += lowbit(x); }}int sum(int x, int n, const vector
    & tr) { int res = 0; while (x > 0) { res += tr[x]; x -= lowbit(x); } return res;}int main() { int n; vector
    a(n + 1); for (int i = 2; i <= n; ++i) { a[i] = 0; } for (int i = 1; i <= n; ++i) { int val; scanf("%d", &val); a[i] = val; } vector
    tr(n + 1, 1); // ???????????????1 vector
    ans(n + 1); for (int i = n; i >= 1; --i) { int k = a[i] + 1; int l = 1, r = n; while (l < r) { int mid = (l + r + 1) / 2; int s = sum(mid, n, tr); if (s >= k) { r = mid; } else { l = mid; } } ans[i] = l; add(l, -1, n, tr); } for (int i = 1; i <= n; ++i) { printf("%d\n", ans[i]); } return 0;}

    ????

  • lowbit????????????????????????????
  • add????????????????????????
  • sum??????????????????????????????????
  • ????
    • ?????????????
    • ?????????????????????
    • ???????????????????????
    • ?????????
  • ?????????????????????????????????

    转载地址:http://satn.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现bubble sort冒泡排序算法(附完整源码)
    查看>>
    Objective-C实现Burke 抖动算法(附完整源码)
    查看>>
    Objective-C实现CaesarsCiphe凯撒密码算法(附完整源码)
    查看>>
    Objective-C实现cartesianProduct笛卡尔乘积算法(附完整源码)
    查看>>
    Objective-C实现check strong password检查密码强度算法(附完整源码)
    查看>>
    Objective-C实现chudnovsky algorithm楚德诺夫斯基算法(附完整源码)
    查看>>
    Objective-C实现circle sort圆形排序算法(附完整源码)
    查看>>
    Objective-C实现CircularQueue循环队列算法(附完整源码)
    查看>>
    Objective-C实现climbStairs爬楼梯问题算法(附完整源码)
    查看>>
    Objective-C实现cocktail shaker sort鸡尾酒排序算法(附完整源码)
    查看>>
    Objective-C实现cocktailShakerSort鸡尾酒排序算法(附完整源码)
    查看>>
    Objective-C实现CoinChange硬币兑换问题算法(附完整源码)
    查看>>
    Objective-C实现collatz sequence考拉兹序列算法(附完整源码)
    查看>>
    Objective-C实现Collatz 序列算法(附完整源码)
    查看>>
    Objective-C实现combinations排列组合算法(附完整源码)
    查看>>
    Objective-C实现combine With Repetitions结合重复算法(附完整源码)
    查看>>
    Objective-C实现combine Without Repetitions不重复地结合算法(附完整源码)
    查看>>
    Objective-C实现conjugate gradient共轭梯度算法(附完整源码)
    查看>>
    Objective-C实现connected components连通分量算法(附完整源码)
    查看>>
    Objective-C实现Connected Components连通分量算法(附完整源码)
    查看>>