思路:

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. mysql字符串的常用函数(截取和拼接)

    #截取字符串(先正序取2个,再倒序取1个)SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('aaa-gg-cc-dd','-',2),'-',-1) #获取子表某个字段的 ...

  2. date 命令 时间戳到标准格式转换

    1. 知道时间戳看标准时间, 时间戳到 秒: Wed Apr :: CST 2. 看到前时间时间戳格式 date +%s 3. 知道某个标准时间, 看时间戳 date -d "Wed Apr ...

  3. ⭐驱动之module_init/module_exit与系统启动关系

    在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...

  4. linux子系统的初始化_subsys_initcall()

    http://my.oschina.net/u/572632/blog/305492#OSC_h1_3

  5. POJ1797 Heavy Transportation —— 最短路变形

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  6. 关于O_DIRECT的那些事儿

    很久之前落下的坑,一直没怎么记录-.- 一般地,如果在Linux内核中读写一个文件,其IO流程都需要经过Kernel内的page cache层次,如果程序员若想要使用自己开发的缓存系统,那么就可以在打 ...

  7. Android的三种主流资源尺寸

    Android三种主流资源屏幕尺寸:QVGA.HVGA.WVGA VGA的分辨率是640x480像素 QVGA(Quarter VGA)就是320x240,即VGA分辨率的1/4 HVGA(Half ...

  8. hdu2552

    点击打开链接 思路: 1.tan(a+b) = ( tan(a) + tan(b) ) / (1 – tan(a) * tan(b) ) 2.tan( atan(x) ) = x arctan(1/s ...

  9. [Java] 继承,隐藏,覆盖,重载,多态,抽象类,接口

    1.子类 class SonClass extends ABC{...} 在子类定义后,子类中就可以直接隐式包含父类的成员变量和方法,而不用再写,这就是使用继承的优点. 子类包含父类的成员,不是子类和 ...

  10. BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流

    BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流 Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1 ...