discretization algorithm

In C++, discretization is the process of mapping elements of a set to consecutive integer values. This process is usually used to solve situations where elements need to be sorted, counted, or otherwise operated, but the value range of the elements is large or there are duplicate values.

The following is an implementation of a commonly used discretization algorithm:
 

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

using namespace std;

vector<int> discrete(vector<int>& nums) {
    // 复制原始数组并排序
    vector<int> sortedArr = nums;
    sort(sortedArr.begin(), sortedArr.end());

    // 去重
    sortedArr.erase(unique(sortedArr.begin(), sortedArr.end()), sortedArr.end());

    // 离散化映射表
    vector<int> mapping(nums.size());

    // 遍历原始数组,查找每个元素在排序后数组中的位置
    for (int i = 0; i < nums.size(); i++) {
        mapping[i] = lower_bound(sortedArr.begin(), sortedArr.end(), nums[i]) - sortedArr.begin();
    }

    return mapping;
}

int main() {
    vector<int> nums = {10, 5, 3, 7, 10, 7};
    vector<int> mapping = discrete(nums);

    cout << "原始数组:";
    for (int num : nums) {
        cout << num << " ";
    }
    cout << endl;

    cout << "离散化后的映射数组:";
    for (int num : mapping) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}



Running results:
```
Original array: 10 5 3 7 10 7
Discretized mapping array: 1 0 4 2 1 2
```

In this example, we convert the original array `{10, 5, 3, 7, 10, 7}` is discretized, and the final mapping array is `{1, 0, 4, 2, 1, 2}`. After discretization, 10 in the original array maps to 1, 5 maps to 0, 3 maps to 4, and 7 maps to 2. The elements in the discretized mapped array can be used to represent the elements in the original array.

In the implementation of the discretization algorithm, we first copy and sort the original array, and then remove duplicate elements in the sorted array. We then iterate through the original array, use the `lower_bound()` function to find the position of each element in the sorted array, and use the found position as an element in the mapped array. Finally, the mapped array is returned.

The time complexity of the discretization algorithm is O(nlogn), where n is the size of the original array. This is because the algorithm needs to sort the original array and uses a binary search when finding the position of each element.
AcWing Exercise-Interval Sum

Events - AcWing

 

#include<bits/stdc++.h>
using namespace std;
const int N=300010;

int a[N],s[N];
int n,m;
vector<int> alls;
vector<pair<int,int>> query,add;
int find(int 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;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        int x,c;
        cin>>x>>c;
        add.push_back({x,c});
        alls.push_back(x);
    }
    for(int i=1;i<=m;i++)
    {
        int l,r;
        cin>>l>>r;
        query.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 k=find(item.first);
        a[k]+=item.second;
    }
    for(int i=1;i<=alls.size();i++)
    s[i]=s[i-1]+a[i];
    for(auto item:query)
    {
        int l=find(item.first),r=find(item.second);
        cout<<s[r]-s[l-1]<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_74153798/article/details/131871507