vector如何实现有序数组?

有序数组维护一个序列 a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an,使得其保持有序,而且支持插入时间为 O ( l o g n ) O(logn) O(logn)的操作。python中有第三方库SortedList,C++并没有这样的stl库。
但我们可以使用vector和lower_bound进行类似的操作:使用lower_bound找到插入的位置,使用vector::insert插入该位置。

如题 洛谷P1168 中位数
正统的做法是维护一个最大堆和一个最小堆,最小堆的数在后半部分(比最大堆大),最大堆的数在前半部分,每次取最大堆的堆顶。但是写起来比较麻烦。
用有序数组可以这么写:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <unordered_map>
#include <algorithm>
#define LT(x) (x * 2)
#define RT(x) (x * 2 + 1)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

int n;
int a[100002];
vi b;   // 有序数组


int main() {
   
    //freopen("in.txt", "r", stdin);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
   
        scanf("%d", &a[i]);
    }
    int m = (n + 1) / 2;
    b.push_back(a[0]);
    printf("%d\n", a[0]);
    for (int i = 1; i < m; ++i) {
   
        int pos = lower_bound(b.begin(), b.end(), a[i * 2 - 1]) - b.begin();
        b.insert(b.begin() + pos, a[i * 2 - 1]);
        pos = lower_bound(b.begin(), b.end(), a[i * 2]) - b.begin();
        b.insert(b.begin() + pos, a[i * 2]);
        printf("%d\n", b[i]);
    }
    return 0;
}

相关推荐

  1. vector如何实现有序数组

    2024-02-11 11:54:01       46 阅读
  2. C++vector简单实现

    2024-02-11 11:54:01       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-11 11:54:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-11 11:54:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-11 11:54:01       87 阅读
  4. Python语言-面向对象

    2024-02-11 11:54:01       96 阅读

热门阅读

  1. VMware16安装CentOS7mini 中遇到的一些问题

    2024-02-11 11:54:01       56 阅读
  2. Linux文本三剑客(1)

    2024-02-11 11:54:01       43 阅读
  3. Python列表中的remove功能及用法举例

    2024-02-11 11:54:01       47 阅读
  4. Linux开发:PAM3 Ubuntu(22.04)安装PAM开发库

    2024-02-11 11:54:01       49 阅读
  5. 贪心算法之田忌赛马,多种语言实现

    2024-02-11 11:54:01       40 阅读
  6. 数组旋转变换分析

    2024-02-11 11:54:01       51 阅读
  7. 【SpinalHDL】2.数据类型SpinalEnum

    2024-02-11 11:54:01       62 阅读
  8. C++入门

    C++入门

    2024-02-11 11:54:01      38 阅读