算法学习笔记——离散化

初识离散化

离散化

离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。

通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。

例如:

原数据:1,999,100000,15;

处理后:1,3,4,2;

原数据:{100,200},{20,50000},{1,400};

处理后:{3,4},{2,6},{1,5};

数据的离散化

有些数据本身很大, 自身无法作为数组的下标保存对应的属性。如果这时只是需要这堆数据的相对属性, 那么可以对其进行离散化处理。当数据只与它们之间的相对大小有关,而与具体是多少无关时,可以进行离散化。

对于题目而言,当数据范围过大但数据数量较少时,可以尝试离散化

操作步骤

  1. 储存所有待离散化的值
  2. 排序
  3. 删除重复元素
  4. 索引元素离散化后对应的值

例题

假定有一个无限长的数轴,数轴上每个坐标上的数都是0。

现在,我们首先进行 n 次操作,每次操作将某一位置x上的数加c。

接下来,进行 m 次询问,每个询问包含两个整数l和r,你需要求出在区间[l, r]之间的所有数的和。

输入格式

第一行包含两个整数n和m。

接下来 n 行,每行包含两个整数x和c。

再接下里 m 行,每行包含两个整数l和r。

输出格式

共m行,每行输出一个询问中所求的区间内数字和。

数据范围

− 1 0 9 ≤ x ≤ 1 0 9 , −10^9≤x≤10^9, 109x109, 1 ≤ n , m ≤ 1 0 5 1≤n,m≤10^5 1n,m105 − 1 0 9 ≤ l ≤ r ≤ 1 0 9 −10^9≤l≤r≤10^9 109lr109 − 10000 ≤ c ≤ 10000 −10000≤c≤10000 10000c10000
输入样例:

3 3
1 2
3 6
7 5
1 3
4 6
7 8

输出样例:

8
0
5

题目链接

思路

x,l,r范围为 [ − 1 0 9 , 1 0 9 ] [−10^9,10^9] [109109]
显然不能使用数组来模拟数轴

而n,m范围为
[ 1 , 1 0 5 ] [1,10^5] [1105]

将x,l,r离散化

此时所需的最大空间仅为 n + 2 ∗ m n+2*m n+2m
这样就可以使用数组来进行处理

步骤如下

typedef pair<int, int> PII;

const int N = 300010;// n+2*m == 300000;

int n, m;
int a[N], s[N];//s[N]为a[N]的前缀和数组

vector<PII> add, ask;//分别储存插入操作数与查询操作数
vector<int> alls; // 存储所有待离散化的值

接下来对待离散化数组排序,以便二分查找

sort(alls.begin(), alls.end()); 

删去重复元素

alls.erase(unique(alls.begin(), alls.end()), alls.end()); 
    //unique返回去重后的序列末尾的下一个,erase进行压缩

unique函数的实现(双指针)

 vector<int>::iterator unique(vector<int> &a) 
 {
    int j = 0;
    for (int i = 0; i < a.size(); i ++ )
         if (!i || a[i] != a[i - 1])
             a[j ++ ] = a[i];
     return a.begin() + j;
}
 alls.erase(unique(alls), alls.end());//去重

离散化

写法1

int find(int x) // 找到第一个大于等于x的位置
{
    int l = 0, r = alls.size() - 1;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (alls[mid] >= x) r = mid;
        else l = mid + 1;
    }
    return r + 1; // 映射到1, 2, ...n
}//从1开始映射,方便处理前缀和

写法2

lower_bound函数(此写法在完整代码的注释部分)

int lower_bound(int *array, int size, int key)

完整代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 300010;

int n, m;
int a[N], s[N];

vector<PII> add, ask;
vector<int> alls; // 存储所有待离散化的值

// 二分求出x对应的离散化的值
int find(int x) // 找到第一个大于等于x的位置
{
    int l = 0, r = alls.size() - 1;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (alls[mid] >= x) r = mid;
        else l = mid + 1;
    }
    return r + 1; // 映射到1, 2, ...n
}//从1开始映射,方便处理前缀和

 vector<int>::iterator unique_by(vector<int> &a) //unique的实现
 {
     int j = 0;
     for (int i = 0; i < a.size(); i ++ )
         if (!i || a[i] != a[i - 1])
             a[j ++ ] = a[i];
     // a[0] ~ a[j - 1] 所有a中不重复的数

    return a.begin() + j;
 }

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ )
    {
        int x, c;
        cin >> x >> c;
        add.push_back({x, c});

        alls.push_back(x);//将欲离散化的数插入待离散化数组
    }

    for (int i = 0; i < m; i ++ )
    {
        int l, r;
        cin >> l >> r;
        ask.push_back({l, r});

        alls.push_back(l);//将欲离散化的数插入待离散化数组
        alls.push_back(r);
    }

    sort(alls.begin(), alls.end()); 
    alls.erase(unique(alls.begin(), alls.end()), alls.end());   
    
    // 处理插入
    for (auto item : add)
    {
        int x = find(item.first);//离散化后的值
        //int x = lower_bound(alls.begin(),alls.end(),item.first)-alls.begin()+1;
        a[x] += item.second;
    }

    // 预处理前缀和
    for (int i = 1; i <= alls.size(); i ++ ) s[i] = s[i - 1] + a[i];

    // 处理询问
    for (auto item : ask)
    {
        int l = find(item.first), r = find(item.second);
        //int l = lower_bound(alls.begin(),alls.end(),item.first)-alls.begin()+1;
        //int r = lower_bound(alls.begin(),alls.end(),item.second)-alls.begin()+1;
        cout << s[r] - s[l - 1] << endl;
    }
    return 0;
}
 
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值