思路:

bit + 离散化。

实现:

 #include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int sum(vector<int> & bit, int i)
{
int ans = ;
while (i) { ans += bit[i]; i -= i & -i; }
return ans;
}
void add(vector<int> & bit, int i, int x)
{
int n = bit.size() - ;
while (i <= n) { bit[i] += x; i += i & -i; }
}
vector<int> countSmaller(vector<int>& nums)
{
int n = nums.size();
vector<int> a(nums.begin(), nums.end()), b(nums.begin(), nums.end());
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for (int i = ; i < n; i++)
b[i] = lower_bound(a.begin(), a.end(), b[i]) - a.begin() + ;
vector<int> bit(n + , ), ans(n, );
for (int i = n - ; i >= ; i--)
{
add(bit, b[i], );
ans[i] = b[i] == ? : sum(bit, b[i] - );
}
return ans;
}
};
int main()
{
vector<int> v;
v.push_back();
v.push_back(-);
v.push_back();
v.push_back();
vector<int> ans = Solution().countSmaller(v);
for (auto it: ans) cout << it << " ";
cout << endl;
return ;
}

leetcode315 Count of Smaller Numbers After Self的更多相关文章

  1. LeetCode315—Count of Smaller Numbers After Self—Java版归并算法

    这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释. 此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更高 ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  4. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  5. [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self

    You are given an integer array nums and you have to return a new countsarray. The counts array has t ...

  6. Count of Smaller Numbers After Self -- LeetCode

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  7. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  8. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  9. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

随机推荐

  1. ios状态栏的一些操作

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //显示 [UIApplication sharedA ...

  2. Python小练习_数据库表数据导出到excel

    需求:只要传入一个表名,就能把所有的数据导入出来,字段名是excel的表头 1.要动态获取到标的字段 cur.descrption能获取到表的字段 fileds = [filed[0] for fil ...

  3. Asp与Asp.Net

    1.Asp是什么? ASP是动态服务器页面(Active Server Page)的英文缩写.是微软公司开发的代替CGI脚本程序的一种应用,它可以与数据库和其它程序进行交互,是一种简单.方便的编程工具 ...

  4. codeforces 696C C. PLEASE(概率+快速幂)

    题目链接: C. PLEASE time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. 【前端】CentOS 7 系列教程之二: 安装 git 最新版

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_2.html 这一篇我们来安装git高版本. 卸载yum安装的旧版本 yum remove git 安装 ...

  6. 深入研究 Java Synchronize 和 Lock 的区别与用法

    在分布式开发中,锁是线程控制的重要途径.Java为此也提供了2种锁机制,synchronized和lock.做为Java爱好者,自然少不了对比一下这2种机制,也能从中学到些分布式开发需要注意的地方.  ...

  7. 数据连接池使用方法 JADI

    1.编写context.xml 文件复制到 WebRoot/META_INF/context.xml <?xml version='1.0' encoding='utf-8'?> < ...

  8. CodeForces 719B Anatoly and Cockroaches (水题贪心)

    题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...

  9. Codeforces - 1114B - Yet Another Array Partitioning Task - 构造 - 排序

    https://codeforces.com/contest/1114/problem/B 一开始叫我做,我是不会做的,我没发现这个性质. 其实应该很好想才对,至少要选m个元素,其中m个作为最大值,从 ...

  10. poj2421【MST-prim+Kruskal】

    水过~~~~打好基础/~~ ------prim #include <iostream> #include <stdio.h> #include <string.h> ...